FileUpload

package cn.zcq100.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
* Servlet implementation class Upload
*/
public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletContext sc;
private String path; // 文件保存路径
private String[] fileType; // 文件类型
private double filesize; // 最大上传大小

/**
* @see HttpServlet#HttpServlet()
*/
public Upload() {
   super();

}

/**
* 根据配置文件初始化上传的参数 文件类型 保存路径 上传文件大小
* 
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
   this.sc = config.getServletContext();
   path = config.getInitParameter("path");
   fileType = config.getInitParameter("fileType").split(",");
   filesize = Integer.parseInt(config.getInitParameter("filesize"));
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
*      response)
*/
protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
   PrintWriter out = response.getWriter();
   out
     .print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
   DiskFileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload fileupload = new ServletFileUpload(factory);

   try {
    List<FileItem> items = fileupload.parseRequest(request);
    for (FileItem item : items) {
     if (checkFile(item)) {
      String filename = item.getName().substring(
        item.getName().lastIndexOf("\\"));
      File f = new File(sc.getRealPath("/") + path + "\\"
        + filename);
      item.write(f);
      out.print("本地文件:" + item.getName() + "<br/>");
      out.print("远程文件:" + f.getAbsolutePath() + "<br/>");
     } else {
      out.print(item.getName() + "上传失败<br/>");
     }
    }
    out.print("<a href='"+request.getContextPath()+"'>返回</a>");
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    out.close();
   }
}

/**
* 检查上传的文件 文件类型 文件大小
* 
* @param item
*            文件
* @return boolean 是否允许上传
*/
private boolean checkFile(FileItem item) {
   boolean flag = false;
   if (!item.isFormField() && item.getSize() <= filesize) {
    String file = item.getName();
    for (int i = 0; i < fileType.length; i++) {
     if (file.endsWith(fileType[i]))
      flag = true;
    }
   }
   return flag;
}

}

 在web.xml中为servlet配置好初始参数

你可能感兴趣的:(apache,jsp,Web,servlet,F#)