在平常的做项目过程中,经常碰到要上传文件的功能,一直都在逃避文件流的写法,总是能找到其他的方法去替代,也就一直没有成功用其他插件实现上传功能。无插件下载实在是太不稳定了,不过原理很容易看懂。用插件的话,之前一直有人说jspsmart很好用,后来上网查了一下,相比之下,commons-fileupload插件还是最好用的。
一直不想去碰,因为总觉得会很麻烦,想要一个简单一点的实例。下面贴出来,我的源代码。方便以后想要上传文件功能的时候可以直接在上面修改。
Jsp页面:
<form action="upload" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<button type="submit">上传</button>
</form>
后台java类:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
public class upload extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getSession().getServletContext().getRealPath("/");
//file upload factory
DiskFileItemFactory factory = new DiskFileItemFactory();
//setting upload file path
factory.setRepository(new File(path));
//set default memory size
factory.setSizeThreshold(1024*1024*10);
//
ServletFileUpload upload1 = new ServletFileUpload(factory);
upload1.setHeaderEncoding("utf-8");//这一句可以解决中文乱码的问题
try {
List<FileItem> list = upload1.parseRequest(request);
for (FileItem item : list) {
if(item.isFormField()){ //过滤掉表单中非文件域
String name = item.getFieldName();//input name
String value = item.getName();//input content
request.setAttribute(name, value);
}else{
String name = item.getFieldName();//input name
String value = item.getName();//input content
value = value.substring(value.lastIndexOf("\\")+1,value.length());
//output file
OutputStream fileOutStream = new FileOutputStream(new File(path,value));
//input file
InputStream fileInputStream = item.getInputStream();
//file buffer
byte [] buffer = new byte[1024];
//read
int length = 0;
while(( length = fileInputStream.read(buffer)) > 0){
fileOutStream.write(buffer,0,length);
}
//close
fileInputStream.close();
fileOutStream.close();
item.write(new File(path,value));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>uploadServlet</servlet-name>
<servlet-class>upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadServlet</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
需要的两个jar包:commons-fileupload-1.2.jar和commons-io.jar