java文件上传

来源:互联网 发布:关于钢琴的软件 编辑:程序博客网 时间:2024/06/05 18:18

文件上传

package com.dmm.android.controller;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;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 org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.dmm.android.common.RootPath;@Controller@ResponseBody@RequestMapping("/form")public class FormController {    private final Logger LOG = Logger.getLogger(FormController.class);    @RequestMapping(value = "/getSC", produces = "multipart/form-data; charset=utf-8")    public void getSC(HttpServletRequest req, HttpServletResponse resp){        String scTitle = null;        System.out.println(ServletFileUpload.isMultipartContent(req)+"================================");        DiskFileItemFactory factory = new DiskFileItemFactory();        ServletFileUpload sfu = new ServletFileUpload(factory);        sfu.setSizeMax(10*1024*1024);        sfu.setHeaderEncoding("utf-8");        List<String> listStr = new ArrayList<String>();        List<FileItem> fileItemList = null;        try {            fileItemList = sfu.parseRequest(req);                Iterator<FileItem> fileItems = fileItemList.iterator();                while(fileItems.hasNext()) {                    FileItem fileItem = fileItems.next();                     if(fileItem.isFormField()){                        String name = fileItem.getFieldName();// name属性值                         String value = fileItem.getString("utf-8");// name对应的value值                        if(name.equals("text")) {                            scTitle = value;                        }                        System.out.println(name + " = " + value);                        LOG.info(name + " = " + value);                    } else {                        String fileName = fileItem.getName();                        System.out.println("原文件名:" + fileName);                        String suffix = fileName.substring(fileName.lastIndexOf('.'));                          System.out.println("扩展名:" + suffix);                        String newFileName = new Date().getTime() + suffix;                        System.out.println("新文件名:" + newFileName);                        listStr.add(newFileName);                        // 5. 调用FileItem的write()方法,写入文件                          String imgPath = "/"+RootPath.getPath()+"/Temp/" + newFileName;                        LOG.info(imgPath);                        File file = new File(imgPath);                          System.out.println(file.getAbsolutePath());                         LOG.info(file.getAbsolutePath());                        fileItem.write(file);                          // 6. 调用FileItem的delete()方法,删除临时文件                          fileItem.delete();                     }                }        } catch (FileUploadException e) {            e.printStackTrace();        } catch (Exception e) {            e.printStackTrace();        }    }}

文件目录

package com.dmm.android.common;import java.io.Serializable;public final class RootPath implements Serializable {  private static final long serialVersionUID = -7178694607074894676L;  private static String path;  private RootPath() {  }  public static String getPath() {    path = new RootPath().getClass().getResource("").toString();    path = path.replace("%20", " ").replaceAll("file:/", "").replaceAll("/WEB-INF.*", "");    return path.replaceAll("^(home)", "/$1");  }  public static String getConfigPath() {    return RootPath.getPath() + "/WEB-INF/config";  }}
原创粉丝点击