📄 fileuploadservlet.java
字号:
package com.eshop.servlet.manager.product;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
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 {
String uploadPath = "D:\\Tomcat 5.5\\webapps\\e-shop\\productPic\\"; // 上传文件的目录
String tempPath = "D:\\Tomcat 5.5\\webapps\\e-shop\\productPic\\temp\\"; // 临时文件目录
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath);
// 得到所有的文件:
List fileItems;
try {
fileItems = fu.parseRequest(request);
Iterator i = fileItems.iterator();
String time = "";
String strExtend = "";
while(i.hasNext()) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
GregorianCalendar cal = new GregorianCalendar();
time = formatter.format(cal.getTime());
FileItem fi = (FileItem)i.next();
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
int j=0;
j=fileName.lastIndexOf('.');
strExtend = fileName.substring(j,fileName.length());
// 在这里可以记录用户和文件信息
// ...
// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
try {
fi.write(new File(uploadPath + time + strExtend));
response.setCharacterEncoding("gbk");
response
.getWriter()
.write(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\" /><html><script language=\"javascript\">window.location.href=\"../manager/fileupload/fileUpload.jsp?product_pic=" + time + strExtend + "\"</script></html>");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//String product_pic=time + strExtend;
/*request.setAttribute("product_pic", product_pic);
RequestDispatcher requestDisptcher=request.getRequestDispatcher("../manager/fileupload/fileUpload.jsp");
requestDisptcher.forward(request, response);*/
response.setCharacterEncoding("gbk");
response
.getWriter()
.write(
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\" /><html><script language=\"javascript\">window.location.href=\"../manager/fileupload/fileUpload.jsp?product_pic=" + time + strExtend + "\"</script></html>");
}} catch (FileUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 依次处理每一个文件:
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -