Upload and download files based SSM framework

We all know that in software development, uploading and downloading files is the most common features, can also be done through this stream javaIO this function, but today we take a look at file-based SSM to upload and download function framework.
First, build a good framework for SSM, refer to my other blog to build a framework for SSM.
1, in the new pro.xml dependent:

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

2, disposed in the spring following code configuration file:

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

3, the front end sends the request to write jsp:

<form action="${pageContext.request.contextPath}/uploadFile"  method="post" enctype="multipart/form-data">  
      选择文件:<input type="file" name="file"/> 
      <input type="submit" value="上传">  
</form> 
<form action="${pageContext.request.contextPath}/downFile" method="get">  
              <input type="submit" value="下载">  
</form>

4, the control layer Code:

 /**文件上传功能*/
    @RequestMapping(value="uploadFile",headers = "content-type=multipart/*",method = RequestMethod.POST)
    public String uploadFile(MultipartFile file,HttpServletRequest request)throws IOException{
        //文件上传的位置,如果不存在uploadFile文件夹就创建一个
        String path = request.getSession().getServletContext().getRealPath("uploadFile");
        System.out.println(request.getSession().getServletContext());
        String fileName=file.getOriginalFilename();
        File dir=new File(path,fileName);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //MultipartFile自带的解析方法
        file.transferTo(dir);
        return "getAllResume";
    }

    //**文件下载功能*//*
    @RequestMapping("downFile")
    public void downFile(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String fileName = request.getSession().getServletContext().getRealPath("uploadFile")+"/yxc.docx";
        InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
        String filename = "yxc.docx";
        filename = URLEncoder.encode(filename,"UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        response.setContentType("multipart/form-data");
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }

The general location of the file upload is the default target in the project which will have inside, if you do not know can print request.getSession (). GetServletContext (). GetRealPath () to get the absolute path, then go. It's just finished uploading and downloading files, there are some details still need to be modified, such as the name of the file to be sure not directly, but to get through the variables associated with the database and if so, you can check the file path by database and file name, and then download or upload. Also note saying, headers = "content-type = multipart / *", if you can not write access That's good, I did not write this sentence on the error will be 415 media types are not supported in the study, which is a request head in question.

Published 33 original articles · won praise 37 · views 4386

Guess you like

Origin blog.csdn.net/weixin_42142899/article/details/103351514