upload.java
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.io.*;
- import java.util.*;
- import java.util.regex.*;
- import org.apache.commons.fileupload.*;
- import org.apache.commons.io.FilenameUtils;
- import org.apache.commons.fileupload.servlet.*;
- import org.apache.commons.fileupload.disk.*;
- import com.axtrip.util.*;
- public class Upload extends HttpServlet {
- private String uploadPath = "C:\\upload\\"; // 用于存放上傳文件的目錄
- private String tempPath = "C:\\upload\\temp\\"; // 用于存放臨時(shí)文件的目錄
- /**
- * Constructor of the object.
- */
- public Upload() {
- super();
- }
- /**
- * Destruction of the servlet.
- */
- public void destroy() {
- super.destroy();
-
- }
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request,response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html; charset=GB2312");
- PrintWriter out=response.getWriter();
- try {
- System.out.println("start");
- DiskFileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setSizeMax(4 * 1024 * 1024);
- //允許上傳的文件大小,4M
- factory.setSizeThreshold(1 * 1024 * 1024);
- //設(shè)置最多只允許在內(nèi)存中存儲(chǔ)的數(shù)據(jù),單位:字節(jié)
- factory.setRepository(new File(tempPath));
- //文件大小超過getSizeThreshold()的值時(shí)數(shù)據(jù)存放在硬盤的目錄
- String[] errorType = { "exe", "com", "cgi", "asp" };
- //過濾掉的文件類型
- List uploadlist=upload.parseRequest(request);
- //開始讀取上傳信息
- Iterator iter=uploadlist.iterator();
- while(iter.hasNext()){
- FileItem item=(FileItem)iter.next();
- if(!item.isFormField()){//忽略其他不是文件域的所有表單信息
- String passandfilename = item.getName();
- //取到客戶端完整 路徑+文件名 c:/111.jpg
- String filename=FilenameUtils.getName(passandfilename);
- //取到 文件名 111.jpg
- String filetype = getFiletype(filename);//取到文件類型 jpg
-
- boolean flag = false;//判斷是否有非法的文件類型
- for(int i =0; i<=errorType.length-1; i++){
- if (filetype.equals(errorType[i])){
- flag = true;
- }
- }
- if(flag)break;//如果有非法文件,退出
-
- if(!filename.equals("")){
- filename = getNewFileNameByTime(filename);//文件重命名
- String savepath=uploadPath+filename;
- out.println("savepath is "+savepath);
- File saveFilepath=new File(savepath);
- item.write(saveFilepath);
- }else{
- out.print("沒東西");
- }
- }
- }
- } catch (FileUploadBase.SizeLimitExceededException e) {
- out.println("文件太大了吧");
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- public void init() throws ServletException {
-
- }
-
- //取文件名后綴
- public static String getFiletype(String fileName) {
- String type = "";
- if (fileName == null || fileName.equals(""))
- return type;
- int position = fileName.lastIndexOf(".");
- if (position != -1) {
- type = fileName.substring(position + 1 , fileName.length());
- }
- return type;
- }
- //將文件重命名
- synchronized public String getNewFileNameByTime(String fileName){
- Date dt = new Date();
- String fType = getFiletype(fileName);
-
- String newFileName = new Date().getTime()+ Math.random() + "." + fType;
- return newFileName ;
- }
-
- }
|