📄 0106.htm
字号:
<html>
<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>如何在Web页上实现文件上传</strong></big></p>
<div align="right">摘自互联网</div>
<p>
public class UploadServlet extends HttpServlet<br>
{<br>
//default maximum allowable file size is 100k<br>
static final int MAX_SIZE = 102400;<br>
//instance variables to store root and success message<br>
String rootPath, successMessage;<br>
/**<br>
* init method is called when servlet is initialized.<br>
*/<br>
public void init(ServletConfig config) throws ServletException<br>
{<br>
super.init(config);<br>
//get path in which to save file<br>
rootPath = config.getInitParameter("RootPath");<br>
if (rootPath == null)<br>
{<br>
rootPath = "/";<br>
}<br>
/*Get message to show when upload is complete. Used only if<br>
a success redirect page is not supplied.*/<br>
successMessage = config.getInitParameter("SuccessMessage");<br>
if (successMessage == null)<br>
{<br>
successMessage = "File upload complete!";<br>
}<br>
}<br>
/**<br>
* doPost reads the uploaded data from the request and writes<br>
* it to a file.<br>
*/<br>
public void doPost(HttpServletRequest request,<br>
HttpServletResponse response)<br>
{<br>
ServletOutputStream out=null;<br>
DataInputStream in=null;<br>
FileOutputStream fileOut=null;<br>
try<br>
{<br>
/*set content type of response and get handle to output<br>
stream in case we are unable to redirect client*/<br>
response.setContentType("text/plain");<br>
out = response.getOutputStream();<br>
}<br>
catch (IOException e)<br>
{<br>
//print error message to standard out<br>
System.out.println("Error getting output stream.");<br>
System.out.println("Error description: " + e);<br>
return;<br>
}<br>
try<br>
{<br>
//get content type of client request<br>
String contentType = request.getContentType();<br>
//make sure content type is multipart/form-data<br>
if(contentType != null && contentType.indexOf(<br>
"multipart/form-data") != -1)<br>
{<br>
//open input stream from client to capture upload file<br>
in = new DataInputStream(request.getInputStream());<br>
//get length of content data<br>
int formDataLength = request.getContentLength();<br>
//allocate a byte array to store content data<br>
byte dataBytes[] = new byte[formDataLength];<br>
//read file into byte array<br>
int bytesRead = 0;<br>
int totalBytesRead = 0;<br>
int sizeCheck = 0;<br>
while (totalBytesRead < formDataLength)<br>
{<br>
//check for maximum file size violation<br>
sizeCheck = totalBytesRead + in.available();<br>
if (sizeCheck > MAX_SIZE)<br>
{<br>
out.println("Sorry, file is too large to upload.");<br>
return;<br>
}<br>
bytesRead = in.read(dataBytes, totalBytesRead,<br>
formDataLength);<br>
totalBytesRead += bytesRead;<br>
}<br>
//create string from byte array for easy manipulation<br>
String file = new String(dataBytes);<br>
//since byte array is stored in string, release memory<br>
dataBytes = null;<br>
/*get boundary value (boundary is a unique string that<br>
separates content data)*/<br>
int lastIndex = contentType.lastIndexOf("=");<br>
String boundary = contentType.substring(lastIndex+1,<br>
contentType.length());<br>
//get Directory web variable from request<br>
String directory="";<br>
if (file.indexOf("name=\"Directory\"") > 0)<br>
{<br>
directory = file.substring(<br>
file.indexOf("name=\"Directory\""));<br>
//remove carriage return<br>
directory = directory.substring(<br>
directory.indexOf("\n")+1);<br>
//remove carriage return<br>
directory = directory.substring(<br>
directory.indexOf("\n")+1);<br>
//get Directory<br>
directory = directory.substring(0,<br>
directory.indexOf("\n")-1);<br>
/*make sure user didn't select a directory higher in<br>
the directory tree*/<br>
if (directory.indexOf("..") > 0)<br>
{<br>
out.println("Security Error: You can't upload " +<br>
"to a directory higher in the directory tree.");<br>
return;<br>
}<br>
}<br>
//get SuccessPage web variable from request<br>
String successPage="";<br>
if (file.indexOf("name=\"SuccessPage\"") > 0)<br>
{<br>
successPage = file.substring(<br>
file.indexOf("name=\"SuccessPage\""));<br>
//remove carriage return<br>
successPage = successPage.substring(<br>
successPage.indexOf("\n")+1);<br>
//remove carriage return<br>
successPage = successPage.substring(<br>
successPage.indexOf("\n")+1);<br>
//get success page<br>
successPage = successPage.substring(0,<br>
successPage.indexOf("\n")-1);<br>
}<br>
//get OverWrite flag web variable from request<br>
String overWrite;<br>
if (file.indexOf("name=\"OverWrite\"") > 0)<br>
{<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -