zoukankan      html  css  js  c++  java
  • SpringMVC 文件上传(Multipart)

    作者QQ:1095737364    QQ群:123300273     欢迎加入!

        

      平时用的上传一般是输入流和输出流上传,今天使用的是transferTo方法:

        Multipart请求是在控制器实例中实现一个Multipart HttpServletRequest接口的request对象,MultipartHttpServletRequest接口简单地扩展了默认的HttpServletRequest接口,并提供一些用来处理请求文件的方法。

               transferTo方法实现文件是通过复制文件来实现,下面是一些Multipart的一些方法:

        • getSize():获得文件长度,以此决定允许上传的文件大小。
        • isEmpty():判断上传文件是否为空文件,以此决定是否拒绝空文件。
        • getInputStream():将文件读取为java.io.InputStream流对象。
        • getContentType():获得文件类型,以此决定允许上传的文件类型。
        • transferTo(dest):将上传文件写到服务器上指定的文件。

       

     下面是具体的示例:  

    引入jar包

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

    SpringMVC 下加入以下代码

    <!-- 需要文件上传功能时,启用以下配置 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxInMemorySize">
            <value>1638400</value>
        </property>
    </bean>

    jsp前端:

    <input name="upgradeFile" type="file">

    Java后台:

    @RequestMapping(value = "/upgrade", method = RequestMethod.POST)
        public ModelAndView versionUpgrade(@RequestParam(value="upgradeFile",required=false) MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
            //保存上传
            String contextpath=request.getContextPath();
            String webappPath=request.getSession().getServletContext().getRealPath("/");
            OutputStream out = null;
            InputStream fileInput=null;
            try{
                if(file!=null){
                    String originalName=file.getOriginalFilename();
                    String type=file.getContentType();
                    String filepath=request.getServletContext().getRealPath("/")+uploadDir+"/"+originalName;         
                    filepath= Utils.format(filepath);
                    File files=new File(filepath);
                   //打印查看上传路径
                    System.out.println(filepath);
                    if(!files.getParentFile().exists()){
                        files.getParentFile().mkdirs();
                    }
                    file.transferTo(files);
                }
            }catch (Exception e){
            }finally{
                try {
                    if(out!=null){
                        out.close();
                    }
                    if(fileInput!=null){
                        fileInput.close();
                    }
                } catch (IOException e) {
                }
            }
    
    
  • 相关阅读:
    [COCI20142015#1] Kamp
    [CEOI2007]树的匹配Treasury
    [JLOI2016/SHOI2016]侦察守卫
    [POI2015]MOD
    [BJOI2017]机动训练
    [九省联考2018]一双木棋chess
    [清华集训2012]串珠子
    [POI2014]ZALFreight
    [SHOI2009]舞会
    [COCI2019]Mobitel
  • 原文地址:https://www.cnblogs.com/yysbolg/p/6544154.html
Copyright © 2011-2022 走看看