FileUploadServlet 文件上传

阅读更多

package com.appdev.bsf.server.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.appdev.bsf.common.client.datasource.DictionaryGwtRpcService;
import com.ibm.icu.text.SimpleDateFormat;

public class FileUploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private DictionaryGwtRpcService service;
    private ServletFileUpload upload;
    private String year;
    private String month;
    private String day;

    @Override
    public void init() throws ServletException {
        FileItemFactory factory = new DiskFileItemFactory();
        upload = new ServletFileUpload(factory);

        ServletContext application;
        WebApplicationContext wac;

        application = getServletContext();
        wac = WebApplicationContextUtils.getWebApplicationContext(application);
        service = (DictionaryGwtRpcService) wac.getBean("dictionaryGwtRpcServiceImpl");

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] dateArr = sdf.format(date).split("-");
        year = dateArr[0];
        month = dateArr[1];
        day = dateArr[2];
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        response.setContentType("text/html;charset=utf-8");

        Integer id = null;
        String clsUrl = null;
        Map data = null;
        try {
            id = Integer.valueOf(request.getParameter("id"));
            clsUrl = request.getParameter("clsUrl");
            data = new HashMap();
        } catch (Exception e) {
            e.printStackTrace();
        }

        List items = null;
        upload.setSizeMax(1024 * 1024 * 2);
        try {

            items = upload.parseRequest(request);
        } catch (FileUploadException ex) {
            throw new ServletException(ex);
        }

        Iterator iter = items.iterator();

        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField() == false) {
                operatorFile(item, id, clsUrl, data);
            }
        }
    }

    private void operatorFile(FileItem item, Integer id, String clsUrl, Map data) {
        String fileName = null;
        // 获得上传路径
        String path = "/uploads/" + year + "/" + month + "/" + day;
        String savePath = getServletContext().getRealPath(path);

        // 获得文件扩展名
        String ext = item.getName().substring(item.getName().lastIndexOf("."));

        // 取的file域的值的名字,不带路径
        int pos = item.getName().lastIndexOf("\\");
        String fieldValue = item.getName().substring(pos + 1);
        // 或直接保存成文件

        // 判断当天的文件夹是否存在,按年/月/日创建,如果不存在则创建
        if (!new File(savePath).isDirectory()) {
            new File(savePath).mkdirs();
        }
        // 判断是否有扩展名exe
        if ("exe".equals(ext)) {
            return;
        }
        File file = new File(savePath, fieldValue);
        try {
            fileName = item.getFieldName();
            // 写文件
            item.write(file);
            // 存路径
            data.put(fileName, path + "/" + item.getName());

            // 数据库写值
            service.updateFiledUrl(clsUrl, id, data);
        } catch (Exception e) {
            e.printStackTrace();
        }// 直接保存文件
    }
}

你可能感兴趣的:(java,servlet,gwt,smartgwt)