import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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.FileUploadException;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public FileUploadServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final long MAX_SIZE = 1024 * 1024 * 1024;// 设置上传文件最大为 1G
// 允许上传的文件格式的列表
final String[] allowedExt = new String[] { "txt" };
response.setContentType("text/html; charset=utf-8");
// 实例化一个硬盘文件工厂,用来配置上传组件ServletFileUpload
DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(4096);// 设置上传文件时用于临时存放文件的内存大小,这里是4K.多于的部分将临时存在硬盘
dfif.setRepository(new File(request.getRealPath("upload/temp/")));// 设置存放临时文件的目录,web根目录下的ImagesUploadTemp目录
// 用以上工厂实例化上传组件
ServletFileUpload sfu = new ServletFileUpload(dfif);
// 设置最大上传尺寸
sfu.setSizeMax(MAX_SIZE);
PrintWriter out = response.getWriter();
// 从request得到 所有 上传域的列表
List fileList = null;
String fullpath="";
String filename="";
String savepath="";
try {
fileList = sfu.parseRequest(request);
} catch (FileUploadException e) {// 处理文件尺寸过大异常
if (e instanceof SizeLimitExceededException) {
out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");
out.println("<a href=\"<%=path %>/<%=request.getAttribute(\"returnjsp\") %>\" class=\"soft2\">返回</a>");
return;
}
e.printStackTrace();
}
// 没有文件上传
if (fileList == null || fileList.size() == 0) {
out.println("请选择上传文件<p />");
out.println("<a href=\"<%=path %>/<%=request.getAttribute(\"returnjsp\") %>\" class=\"soft2\">返回</a>");
return;
}
// 得到所有上传的文件
Iterator fileItr = fileList.iterator();
int i = 0;
// 循环处理所有文件
while (fileItr.hasNext()) {
FileItem fileItem = null;
String path = null;
long size = 0;
// 得到当前文件
fileItem = (FileItem) fileItr.next();
i++;
// 忽略简单form字段而不是上传域的文件域(<input type="text" />等)
if (fileItem == null || fileItem.isFormField()) {
continue;
}
// 得到文件的完整路径
path = fileItem.getName();
// 得到文件的大小
size = fileItem.getSize();
if ("".equals(path) || size == 0) {
out.println("请选择上传文件<p />");
out.println("<a href=\"<%=path %>/<%=request.getAttribute(\"returnjsp\") %>\" class=\"soft2\">返回</a>");
return;
}
// 得到去除路径的文件名
String t_name = path.substring(path.lastIndexOf("\\") + 1);
// 得到文件的扩展名(无扩展名时将得到全名)
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
// 拒绝接受规定文件格式之外的文件类型
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
out.println("请上传以下类型的文件<p />");
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
out.println("*." + allowedExt[allowFlag]
+ " ");
out.println("<a href=\"<%=path %>/<%=request.getAttribute(\"returnjsp\") %>\" class=\"soft2\">返回</a>");
return;
}
long now = System.currentTimeMillis();
// 根据系统时间生成上传后保存的文件名
String prefix = String.valueOf(now);
// 保存的最终文件完整路径,保存在web根目录下的ImagesUploaded目录下
String u_name = request.getRealPath("upload/")+"/" + t_name;
//保存文件之前,查看目录下是否有重名文件,如果有,删除
File existfile = new File(u_name);
if(existfile.exists()){
existfile.delete();
System.out.println(" file:"+u_name+" is deleted.");
}
//System.exit(0);
try {
// 保存文件
fileItem.write(new File(u_name));
out.println("<a href=\"<%=path %>/<%=request.getAttribute(\"returnjsp\") %>\" class=\"soft2\">返回</a>");
} catch (Exception e) {
e.printStackTrace();
}
fullpath = u_name;
filename = t_name;
savepath = request.getRealPath("upload/");
}
request.setAttribute("fullpath", fullpath);
request.setAttribute("filename", filename);
request.setAttribute("savepath", savepath);
System.out.println("servlet,fullpath:" + fullpath);
System.out.println("servlet,filename:" + filename);
System.out.println("servlet,savepath:" + savepath);
System.out.println("finish upload file servlet method...");
response.sendRedirect(request.getContextPath()+"/upload/uploadok.jsp?fullpath="+fullpath+"&filename="+filename+"&savepath="+savepath);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
分享到:
相关推荐
http://blog.csdn.net/alanchen520/article/details/34409913
jar包资源分享,你懂的: com.springsource.org.apache.commons.fileupload-1.2.0.jar
springmvc上传文件必备的两个jar包-commons.fileupload-1.2.1.jar和commons.io-1.4.0.jar,导入即可
抽取了apache commons.fileupload的最基本代码,组合成一个简单的文件上传组件
下面举例介绍如何使用它的文件上传功能。 所使用的fileUpload版本为1.2,环境为Eclipse3.3+MyEclipse6.0。FileUpload 是基于 Commons IO的,所以在进入项目前先确定Commons IO的jar包(本文使用commons-io-1.3.2.jar...
本实现抽取了apache commons.fileupload的最基本代码,组合成一个简单的文件上传组件,对parseRequest方法进行了改进,对于文件,直接存储到指定的上传目录,加快了上传速度。使用本程序时,注意上传端的参数设置,...
1、利用org.apache.commons.fileupload编写的文件上传和下载的前台和后台代码,前台代码是Jsp,后台是用servlet处理,共2个例子,各有不同,比较使用; 2、有用jspSmartupload制作一个文件上传和下载的例子,其制作...
NULL 博文链接:https://weideming.iteye.com/blog/1229692
利用org.apache.commons.fileupload编写的文件上传和下载的前台和后台代码,共两个例子,个有不同,费用使用。另有用jspSmartupload制作一个文件上传和下载的例子,其制作的例子能够上传到数据库中。
java上传文件锁必须的jar包,org.apache.commons.fileupload-1.2.2.LIFERAY-PATCHED-1
commons-io-1.4.jar org.apache.commons.fileupload
利用servlet实现的commons-fileupload的实例。也就是文件上传实例。
java+servlet+commons-io-2.4.jar+commons-fileupload-1.3.jar实现文件的上传与下载
commons-fileupload apache开源项目,实现文件上传下载功能 ssh上传时必配
commons-fileupload-1.3.1库文件下载,用于实现文件上传
使用commons-fileupload实现的文件上传和下载,上传和下载都没有中文乱码问题
使用commons-fileupload上传文件 使用commons-fileupload上传文件
NULL 博文链接:https://jickcai.iteye.com/blog/351523
org.apache.commons-fileupload-1.2.1.jar及上传文件的Demo
文件上传组件commons-fileupload用户指南文件上传组件commons-fileupload用户指南