java文件上传

文件上传

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";
  }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值