Spring mvc,uploadifive 文件上传实践

1、前台页面: 引入js和css

<link type="text/css" rel="stylesheet" href="<%=basePath%>static/js/uploadifive/uploadifive.css" />
<script type="text/javascript" src="<%=basePath%>static/js/uploadifive/jquery.min.js"></script>
<script type="text/javascript" src="<%=basePath%>static/js/uploadifive/jquery.uploadifive.min.js"></script>

2、控件初始化:

<script type="text/javascript">
  $(function() {
    $('#file_upload').uploadifive({
      'auto'             : false,
      'buttonClass': 'btn btn-primary',
      'buttonText': "选择文件",
      'queueID'          : 'uploadqueue',
      'uploadScript'     : '/tools/uploadfiles',
      'onUploadComplete' : function(file, data) { console.log(data); }
    });
  });
</script>

3、body页面内容:

<div id="uploadqueue">
<input id="file_upload" name="file_upload" type="file" multiple="true">
<a href="javascript:$('#file_upload').uploadifive('upload')">上传文件</a> |
 <a href="javascript:$('#file_upload').uploadifive('stop')">停止上传!</a>
</div>

4、后台Controller:

@RequestMapping(value = "/uploadfiles")
    @ResponseBody
    public JSONArray uploadFile(HttpServletRequest request){

        Calendar calendar=Calendar.getInstance();
        List<String> filePathList = new ArrayList<String>();
        String filePath= SysConstant.UPLOADPATH;
        String filePathUrl=SysConstant.UPLOADPATHURL+"/"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH);
        filePath=filePath+File.separatorChar+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH);
        try {
            filePathList = FileUploadUtil.uploadFile(request, filePath,filePathUrl);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(Exception ex){
            ex.printStackTrace();
        }

        if(filePathList.size() == 0){
            //return "系统错误";
        }
        return JSONArray.fromObject(filePathList);
    }

5、上传类FileUploadUtil.java

public class FileUploadUtil {
    private static final Logger log = LoggerFactory.getLogger(FileUploadUtil.class);

    public static List<String> uploadFile(HttpServletRequest request, String filePath,String filePathUrl) throws FileNotFoundException {
        List<String> filePathList = new ArrayList<String>();

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        String fileName = null;
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {

            MultipartFile mf = entity.getValue();
            fileName = mf.getOriginalFilename();
            String newfilepath;
            newfilepath = filePath + File.separatorChar + fileName;


            System.out.println("newfilepath=" + newfilepath);
            File dest = new File(filePath);
            if (!dest.exists()) {
                dest.mkdirs();
            }
            File uploadFile = new File(newfilepath);
            if (uploadFile.exists()) {
                uploadFile.delete();
            }
            try {

                log.info("start upload file: " + fileName);
                FileCopyUtils.copy(mf.getBytes(), uploadFile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                // e.printStackTrace();
                log.info("upload failed. filename: " + fileName + e.getMessage());
                return null;
            }
            filePathList.add(filePathUrl+"/"+fileName);
        }

        return filePathList;
    }
}

6、配置spring-mvc.xml文件:

<!-- 文件上传表单的视图解析器 -->
<bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- set the max upload size100MB -->
        <property name="maxUploadSize">
            <value>104857600</value>
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

7、需要增加的jar文件:

<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

总结:这次上传花了一点时间,遇到几个小问题。所以留下来供大家参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值