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>

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Taboos_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值