首页 > 代码库 > 简单的FileUpload文件上传

简单的FileUpload文件上传

引入jar包:commons-fileupload-1.3.1.jar,commons-io-1.3.2.jar

前台HTML:

<form action="./upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td><label>用户名:</label></td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td><label>密码:</label></td>
<td><input type="text" name="userPwd"/></td>
</tr>
<tr>
<td><label>文件名:</label></td>
<td><input type="file" name="fileName1"/></td>
</tr>
<tr>
<td><label>文件名:</label></td>
<td><input type="file" name="fileName2"/></td>
</tr>
<tr>
<td><input type="submit" value="http://www.mamicode.com/提交"/></td>
<td><input type="reset" value="http://www.mamicode.com/重置"/></td>
</tr>
</table>
</form>

后台配置的servlet:

public class FileUpload extends HttpServlet {
private String uploadPath = "C:\\upload\\"; // 上传文件的目录

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.doGet(req, resp);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

try {
// 判断是否有文件上传
boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
if (isMultiPart) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
// factory.setSizeThreshold(yourMaxMemorySize);
// factory.setRepository(yourTempDirectory);
ServletContext context = this.getServletConfig()
.getServletContext();
File repository = (File) context
.getAttribute("javax.servlet.context.tempdir");
factory.setRepository(repository);
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
//upload.setSizeMax(yourMaxRequestSize);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while(iter.hasNext()){
FileItem item = iter.next();

if(item.isFormField()){
String name = item.getFieldName();
String value = http://www.mamicode.com/item.getString();
System.out.println(name+":"+value);
}else{
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();

System.out.println("fieldName:"+fieldName);
System.out.println("fileName:"+fileName);
System.out.println("contentType:"+contentType);
System.out.println("isInMemory:"+isInMemory);
System.out.println("sizeInBytes:"+sizeInBytes);

File uploadedFile = new File(uploadPath+System.currentTimeMillis()+".txt");
item.write(uploadedFile);

//InputStream uploadedStream = item.getInputStream();
//...
//uploadedStream.close();
// Process a file upload in memory
//byte[] data = http://www.mamicode.com/item.get();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

web.xml文件配置:

<servlet>
<servlet-name>FileUpload</servlet-name>
<servlet-class>com.jt.fileupload.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUpload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>

简单的FileUpload文件上传