com.css.common.upload.servlet关于文件上传的代码

package com.css.common.upload.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.css.common.image.ImageUtil;
import com.css.common.util.FileUploadUtil;

@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet {

 @Override
 public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  this.doPost(request, response);
 }
 
 @Override
 @SuppressWarnings("unchecked")
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        DiskFileItemFactory fac = new DiskFileItemFactory();
       
        ServletFileUpload upload = new ServletFileUpload(fac);
       
        upload.setHeaderEncoding("utf-8");
       
        List fileList = null;
       
        try {
            fileList = upload.parseRequest(request);
        } catch (FileUploadException ex) {
         response.getWriter().print("ERROR:" + ex.getMessage());
         return;
        }
       
        Iterator<FileItem> it = fileList.iterator();
       
        /** 上传路径 */
        String upload_path = "";
        /** 返回类型 */
        String return_type = "";
        /** 用于处理多个文件 */
        List<FileItem> itemList = new ArrayList<FileItem>();
       
        while (it.hasNext()) {
         FileItem item = it.next();
         if (item.isFormField()) {
          String formFileName = item.getFieldName();
          if ("upload_path".equals(formFileName)) {
           upload_path = item.getString();
          } else if ("return_type".equals(formFileName)) {
           return_type = item.getString();
          }
         } else {
             itemList.add(item);
            }
        }
       
        if (! upload_path.endsWith("/")) {
         upload_path += "/";
        }
       
        upload_path += FileUploadUtil.getCurrentYMR("/");
       
        File dir = new File(upload_path);
        if (! dir.exists()) {
         dir.mkdirs();
        }
       
        /** 用于返回页面显示 多个之间","分割 */
        String returnString = "";
       
        for (FileItem fileItem : itemList) {
         String fileName = fileItem.getName();
           
            if (fileName == null || fileName.trim().equals("")) {
                return;
            }
           
            String postfix = "";
            if (fileName.lastIndexOf(".") >= 0) {
             postfix = fileName.substring(fileName.lastIndexOf(".") + 1);
            }
           
            /** 文件名称 */
            fileName = FileUploadUtil.getCurrentYMR("_") + "_" + FileUploadUtil.createUnique() + "." + postfix;
           
            /** 文件绝对路径 */
            String filePath = upload_path + "/" + fileName;
           
            File saveFile = new File(filePath);
            try {
             
             fileItem.write(saveFile);
             
             /** 1 表示上传文件并返回[文件名.扩展名]
              *  2 表示上传图片并生成(商品封面图片)缩略图
              *  3 表示上传图片并生成(商品相册图片)缩略图
              *  4 表示上传图片并生成(会员头像)缩略图
              */
             if ("1".equals(return_type)) {
           returnString = returnString + FileUploadUtil.getCurrentYMR("/") + "/" + fileName;
          } else if ("2".equals(return_type)) {
           String width_height = ImageUtil.getInstance().getWidthHeight(filePath);
           ImageUtil.getInstance().createGoodsImg(filePath, filePath + "_" + width_height, postfix, width_height);
           returnString = returnString + FileUploadUtil.getCurrentYMR("/") + "/" + fileName + "_" + width_height + ",";
          } else if ("3".equals(return_type)) {
           String width_height = ImageUtil.getInstance().getWidthHeight(filePath);
           ImageUtil.getInstance().createGoodsAlbumImg(filePath, filePath + "_" + width_height, postfix, width_height);
           returnString = returnString + FileUploadUtil.getCurrentYMR("/") + "/" + fileName + "_" + width_height + ",";
          } else if ("4".equals(return_type)) {
           String w_h = ImageUtil.getInstance().createHeadpicImg(filePath, filePath, postfix);
           returnString = returnString + FileUploadUtil.getCurrentYMR("/") + "/" + fileName + "&" + w_h + ",";
          }
             
            } catch (Exception e) {
             response.getWriter().print("ERROR:" + e.getMessage());
             return;
            }
        }
       
        /** 多张图片 则去掉后面"," */
        if (returnString.endsWith(",")) {
         returnString = returnString.substring(0, returnString.length() - 1);
        }
       
        response.getWriter().print("SUCCESS:" + returnString);
    }
}

 

 

上一篇:【错误记录】前台进程报错 ( Bad notification for startForeground invalid channel for service notification )


下一篇:python综合练习1-- 用户登录