SpringMVC的文件上传下载(同步和异步)

本文介绍了如何在SpringMVC中实现文件的上传和下载,包括单个文件的同步上传及展示,以及多个文件的异步并发上传。通过前端JSP代码展示了具体的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pom.xml 配置

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

<dependency>  
    <groupId>commons-fileupload</groupId>  
    <artifactId>commons-fileupload</artifactId>  
    <version>1.3</version>  
</dependency> 

**springMVC.xml配置**

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10240000000"/>
<property name="defaultEncoding" value="utf-8"/>
</bean>.

单个文件上传(上传后并显示出来)
前端JSP代码
//显示区域

 </div>请选择上传文件<br>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
<script src="scripts/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $(function(){
     $("#buttonFile").click(function(){
         var formData = new FormData();
         formData.append("file",$("#myFile")[0].files[0]);
         alert(formData);
            $.ajax({
                url:'ajax_fileupload',
                type:'POST',
                data:formData,
                dataType:'json',
                processData:false,
                contentType:false,
                success:function(json){
                    alert(json.url);
                    var img="<img src='http://localhost:8080/SpringMVC-Mybatis"+json.url+"' width='400' height='400'/>";
                    $("#myImg").html(img);
                },
                error:function(json) {
                    alert("文件上传失败");
                }
            });  
     });
 });
</script>
    后台Java代码
    //ajax文件上传
@ResponseBody
@RequestMapping(value = "ajax_fileupload",method=RequestMethod.POST)
public String ajaxFileUpload(MultipartFile file,HttpServletRequest request) {
    Boolean mess = false;
    //解决文件名冲突
    String fileName = file.getOriginalFilename();
    String newFileName = UUID.randomUUID().toString()+fileName.substring(fileName.indexOf("."),fileName.length());
    String url = request.getSession().getServletContext().getRealPath("upload");
    System.out.println("url="+url);
    //如果目录不存在就创建一个
    if(!new File(url).exists()) {
        new File(url).mkdirs();
    }
    try {
        FileUtils.copyInputStreamToFile(file.getInputStream(), new File(url + File.separator + newFileName));
        mess = true;
    }catch(Exception e) {
        e.printStackTrace();
        mess = false;
    }
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("mess", mess);
    jsonObject.put("url", File.separator+"upload"+File.separator+newFileName);
    return jsonObject.toJSONString();

}
“`
多个文件异步同时上传
前端JSP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值