SpringMvc实现文件上传

3 篇文章 0 订阅
2 篇文章 0 订阅

首先引入SpringMvc对文件上传的jar包。

commons-io

commons-fileupload

这个可以自行下载.


接着在你的Spirng配置文件中写入一个Bean:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="utf-8"></property>   
        <property name="maxUploadSize" value="10485760000"></property>  
        <property name="maxInMemorySize" value="40960"></property>  
   </bean> 
其中bean ID 的意思是 DispatcherServlet这个前端控制器会查找multipart解析器的时候, 它将会查找ID名为multiparResolver的Bean 所以ID名不能改。




写一个JSP页面, 在里面写一个表单,  我自己的机上把JSP命名为FileInput.jsp, 文件上传表单和普通表单不同

的是他得声明为MIME类型。 在form属性里面设置 enctype=""multipart/form-data"  另外对服务器的请求方法为method="post".  在input标签里面新增一个类型 类型名为 file, 那么视图层就是这样了。

关于action属性, 填上你的项目的url, 如我的项目url为: 那么我下面的FileInput.jsp对应的就是 /zd_web/user/fileUpload  当然方式不止这一种, 不过这一种简单、


FileInput.jsp:

 <%@ page language="java" contentType="text/html; charset=UTF-8"  
        pageEncoding="UTF-8"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>文件上传</title>  
    </head>  
    <body>  
        <form action="/zd_web/user/fileUpload" method="post" enctype="multipart/form-data">  
            <input type="file" name="fileUpload" />  
            <input type="submit" value="上传" />  
        </form>  
    </body>  
    </html>  


接着在你的项目里面写一个Class  我这里命名的是Filecontorller

package login;
import java.io.File;  
import java.io.IOException;  
import java.util.Iterator;  
  
import javax.servlet.http.HttpServletRequest;  
  
import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.multipart.MultipartFile;  
import org.springframework.web.multipart.MultipartHttpServletRequest;  
import org.springframework.web.multipart.commons.CommonsMultipartResolver;  
  
@Controller()  
@RequestMapping("user")  
public class FileController {  
  
 
    @RequestMapping(value = "fileUpload")  
    public String fileUpload2(HttpServletRequest request)throws IllegalStateException, IOException {  
       
   
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(  
                request.getSession().getServletContext());  
  
      
    
        if (multipartResolver.isMultipart(request)) {  
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
        
            Iterator<String> iter = multiRequest.getFileNames();  
           
            while (iter.hasNext()) {  
              
                MultipartFile file = multiRequest.getFile(iter.next());  
                if (file != null) {  
                    String fileName = "demo" + file.getOriginalFilename();  
                    String path = "D:\\" + fileName;            
                    File localFile = new File(path);
                    file.transferTo(localFile);  
                }  
  
            }  
        }  
        return "FileInput";  
    }  
}  
其中Commonsmultipartresolver是一个上传文件的分解器,当客户端的请求到达SpringMvc,SpringMvc就会检查对应的表单有没有设置多媒体类型(我们在刚刚那个表单设置

了MIME类型 如果不知道这个的可以参考HTTP权威指南),当检测到有媒体类型时, 就会解析。   



MultiparHttpServletRequest扩展了HttpServletRequest的接口, 增加了一些文件的处理。 (参考api)



      我在F盘里面上传, 上传的位置位于我的D盘, 我给原上传的文件加上了demo以进行测试。已成功运行。 另外不要GET请求, 因为GET上传的数据默认太小, 我测试了一下根本进不了if  也不要在RequestMapping里面设置方法为Post 表单设置就行。 不然你会出现405.......




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值