SpringMVC(上传下载)UploadDownload

Posted Taboos_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMVC(上传下载)UploadDownload相关的知识,希望对你有一定的参考价值。

编译工具:IntelliJ IDEA 2019.3.5

 代码--------------------------------------------------------------------------------------------

package cn.hp.UploadDownload;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

@Controller
public class UploadDownload 
    @RequestMapping("/upload")
    public String upload(MultipartFile file[], HttpServletRequest request) throws IOException 
        String path = request.getServletContext().getRealPath("/upload/");

        for (int i=0; i<file.length;i++)

            String name = file[i].getOriginalFilename();

            String newName=new Date().getTime()+new Random().nextInt(999999)+name;

            File f=new File(path+newName);

            file[i].transferTo(f);
        
        return "success";
    

    @RequestMapping("/download.do")
    public ResponseEntity<byte[]>download(@RequestParam("fileName")String fileName,HttpServletRequest request ) throws IOException 

        String path=request.getServletContext().getRealPath("/download/");

        File f=new File(path+fileName);

        String newName=new String(fileName.getBytes("utf-8"),"iso8859-1");

        HttpHeaders hh=new HttpHeaders();
        hh.setContentDispositionFormData("attachment",newName);
        hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(f),hh, HttpStatus.CREATED);


    

--------------------------------------------------------------------------------------------------------

  代码--------------------------------------------------------------------------------------------

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
<form action="upload" method="post" enctype="multipart/form-data">
  文件上传<input type="file" name="file"/><br/>
  文件上传<input type="file" name="file"/><br/>
  文件上传<input type="file" name="file"/><br/>
  文件上传<input type="file" name="file"/><br/>
  文件上传<input type="file" name="file"/><br/>
  <input type="submit" value="确定"/>
</form>
  <a href="download/0.png">下载文件png</a>
  <a href="download/name.txt">下载文件txt</a>
  <a href="download/test4.txt">下载文件txt</a>
  <a href="download/自我介绍.docx">下载文件docx</a>
  <a href="download.do?fileName=test4.txt">下载文件</a>
  </body>
</html>

--------------------------------------------------------------------------------------------

需要的jar包链接

https://download.csdn.net/download/Taboos_/19465556?spm=1001.2014.3001.5503

spring.xml文件用到的代码部分

 代码--------------------------------------------------------------------------------------------

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

</bean>

-----------------------------------------------------------------------------------------------------

web.xml代码

 代码--------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">


    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化时加载springmvc配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:*.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>fileEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>Encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>fileEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

-----------------------------------------------------------------------------------------------------

以上是关于SpringMVC(上传下载)UploadDownload的主要内容,如果未能解决你的问题,请参考以下文章

springmvc怎么上传文件

SpringMVC:SpringMVC文件下载

SpringMVC:SpringMVC文件下载

springmvc 多文件上传 MultipartFile 怎么获取前台传过来的参数

SpringMVC:SpringMVC上传实例讲解

SpringMVC:SpringMVC上传实例讲解