FileUpload.java 上传处理页面(Servlet)
java 代码
- package it;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.util.*;
- import java.util.regex.*;
- import org.apache.commons.fileupload.*;
- import org.apache.commons.fileupload.disk.*;
- import org.apache.commons.fileupload.servlet.*;
- public class FileUpload extends HttpServlet {
- private String uploadPath = "d:\\addnetFile\\";
- private File tempPath = new File("c:\\temp\\");
- 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 {
- response.setContentType("text/html;charset=gb2312");
- PrintWriter out = response.getWriter();
- DiskFileItemFactory factory = new DiskFileItemFactory();
- factory.setSizeThreshold(1048576);
- factory.setRepository(tempPath);
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setSizeMax(1073741824);
- List fileitems = null;
- try{
- fileitems =upload.parseRequest(request);
- Iterator iter = fileitems.iterator();
- String regExp = ".+\\\\(.+)";
- String[] errorType = {".exe",".com",".cgi",".asp"};
- Pattern p = Pattern.compile(regExp);
- while(iter.hasNext()){
- FileItem item = (FileItem)iter.next();
- if(!item.isFormField()){
- String name = item.getName();
- long size = item.getSize();
- if((name == null||"".equals(name))&&size == 0)
- continue;
- Matcher m = p.matcher(name);
- boolean result = m.find();
- if(result){
- for(int temp = 0;temp<errorType.length;temp++){
- if(m.group(1).endsWith(errorType[temp])){
- throw new IOException(name +"wrong type");
- }
- }
- try{
- item.write(new File(uploadPath,m.group(1)));
- out.println(name+"    "+size+"<br>");
- }catch(Exception e){
- out.println("333"+e);
- }
- }
- else{
- throw new IOException("fail to upload");
- }
- }
- }
- }catch(IOException e1){
- out.println("222"+e1);
- }catch(FileUploadException e2){
- out.println("111"+e2);
- }
- }
- /**
- * 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 {
- doGet(request,response);
- }
- /**
- * Initialization of the servlet. <br>
- *
- * @throws ServletException if an error occure
- */
- public void init() throws ServletException {
- if(!new File(uploadPath).isDirectory()){
- new File(uploadPath).mkdir();
- }
- if(!tempPath.isDirectory()){
- tempPath.mkdir();
- }
- }
- }