commons-fileupload应用
javaweb项目经常需要文件的上传下载,今天介绍一种简单的apache 的组件来实现文件上传comm首先需要两个jar包的支持commons-fileupload-1.3.jar commons-io-2.4.jar放入WEB_INF的lib包下
上传的界面代码:
upload.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>File upload</title>
</head>
<body>
<form name="myform" action="upload.jsp" method="post"
enctype="multipart/form-data">
Text1:<br>
<input type="text" name="name"><br>
File2:<br>
<input type="file" name="myfile"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
上传的处理jsp代码:
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GB18030"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
String uploadPath="f:\\fileupload";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart==true){
try{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);//得到所有的文件
Iterator<FileItem> itr = items.iterator();
while(itr.hasNext()){//依次处理每个文件
FileItem item=(FileItem)itr.next();
String fileName=item.getName();//获得文件名,包括路径
if(item.isFormField()){
System.out.println("name ============== " + item.getFieldName()+new String(item.getString().getBytes("iso8859-1"),"GBK"));
}
else if(fileName!=null){
File savedFile=new File(uploadPath,item.getName());
item.write(savedFile);
}
}
out.print("upload succeed");
}
catch(Exception e){
e.printStackTrace();
}
}
else{
out.println("the enctype must be multipart/form-data");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>File upload</title>
</head>
<body>
</body>
</html>
upload2.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>File upload</title>
</head>
<body>
<form name="myform" action="servlet/UploadServlet" method="post"
enctype="multipart/form-data">
Text1:<br>
<input type="text" name="name"><br>
File2:<br>
<input type="file" name="myfile"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
下面使用servle来处理上传:
UploadServlet.java
package com.zj.sample;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
private String uploadPath = "f:\\fileupload"; // 上传文件的目录
private String tempPath = "f:\\fileupload\\buffer\\"; // 临时文件目录
File tempPathFile;
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (fi.isFormField()) {
System.out.println("name ============== " + fi.getFieldName()+new String(fi.getString().getBytes("iso8859-1"),"GBK"));
} else {
String fileName = fi.getName();
if (fileName != null) {
File savedFile = new File(uploadPath,fileName);
fi.write(savedFile
补充:软件开发 , Java ,
上一个:Hibernate 注解
下一个:shell_awk
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值