JavaSpringMVC上传下载

第一步:在lib里面添加如下jar包:

commons-fileupload-1.2.2jar

commons-io-2.4.jar

 

 

 或者在pom.xml导入如下xml:

<!-- 文件上传 start -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<!-- 文件下载 end -->

 

第二步:在SpringMVC的配置文件中,配置处理文件上传的bean

<!-- 用来处理文件上传下载 -->

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <!-- 上传文件的最大大小 -->
  <property name="maxUploadSize" value="100000000000"></property>
  <!-- 上传文件的编码方式 -->
  <property name="defaultEncoding" value="UTF-8"></property>
</bean>

 

第三步:

  1、上传的jsp:

<!-- 文件上传需要向form表单添加属性 enctype="multipart/form-data"  -->
<!-- 这表示上传的将会是二进制流的格式,依规定的二进制进行上传,便于服务器处理,使用post请求 -->
<form action="file1/upload" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>请选择文件:</td>
            <td><input type="file" name="file1"></td>
        </tr>
        
        <tr>
            <td colspan="2" align="right"><input type="submit" value="上传"></td>
        </tr>
    </table>
</form>

  2、下载的jsp:

<form action="file1/download?imgs1=imgs/1.jpg" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td><img src="imgs/1.jpg" width="100px"
        height="100px" name="imgs1"></td>
            <td><input type="submit" value="下载"></td>
        </tr>
    </table>
</form>    

 

 

第四步:在Controller写文件上传下载:

@Controller
@RequestMapping(value = "/file1")
public class FileUpload {
    @RequestMapping(value = "/upload")
    public void shangchuan(MultipartFile file1, HttpServletRequest req) {
        try {
            String tomcatpath = req.getServletContext().getRealPath("/imgs");
            File newFile = new File(tomcatpath, file1.getOriginalFilename());//文件路径名称:file1.getOriginalFilename()
            FileUtils.copyInputStreamToFile(file1.getInputStream(), newFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value="/download")
    public void download(String imgs1,HttpServletRequest req,HttpServletResponse resp) {
        
        try {
            String fileName=imgs1.substring(imgs1.lastIndexOf("/")+1);
            
            String uppath=req.getServletContext().getRealPath("/imgs");
            File file=new File(uppath+"/"+fileName);
            fileName=URLEncoder.encode(fileName, "utf-8");
            resp.setHeader("Content-Disposition","attachment;filename=\""+fileName+"\"");
            FileUtils.copyFile(file, resp.getOutputStream());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    

 

上一篇:SpringBoot访问Redis报错java.lang.NoSuchMethodError: redis.clients.jedis.JedisPoolConfig.setMinEvictableI


下一篇:介绍几个好用的工具类