upload and download

front desk:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>

<form action="load/up" method="post" enctype="multipart/form-data" >
    上传<input type="file" name="file"><br/>
  <%--  上传<input type="file" name="file"><br/>--%>

    <input type="submit" value="提交">
</form>

<a href="download/腾讯视频.lnk">下载</a>

<a href="load/download?filename=ryc.jpg">下载2</a>






</body>
</html>

Backstage:

package com.ryc.controller;

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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Random;

@Controller
@RequestMapping("/load")
public class UpDown {
/*
* 循环  多个 文件上传
* */
 /*   @RequestMapping("/up")
    public String up(@RequestParam MultipartFile[]  file , HttpServletRequest request ){
        for (int c = 0 ; c<file.length ; c++) {
            //获取上传路径
            String path = request.getServletContext().getRealPath("/upload/");
            //获取 上传的文件名
            String name = file[c].getOriginalFilename();
            //改名字
            Date a = new Date();
            Random sj = new Random(10000);
            String newname = a.getTime() + sj.nextInt() + name;
            System.out.println(newname);
            //获取 上传的  文件
            File f = new File(path + newname);
            //上传
            try {
                file[c].transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "success";
    }*/

    /*
    *单个文件上传
    * */

/*    @RequestMapping("/up")
    public String up(MultipartFile file , HttpServletRequest request ){

            //获取上传路径
            String path = request.getServletContext().getRealPath("/upload/");
            //获取 上传的文件名
            String name = file.getOriginalFilename();
            //改名字
            Date a = new Date();
            Random sj = new Random(10000);
            String newname = a.getTime() + sj.nextInt() + name;
            System.out.println(newname);
            //获取 上传的  文件
            File f = new File(path + newname);
            //上传
            try {
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        return "success";
    }*/

    @RequestMapping("/download")
    public ResponseEntity<byte[]> downloads(@RequestParam("filename") String filename,HttpServletRequest request) throws IOException {
        //下载的路径
        String path = request.getServletContext().getRealPath("/download/");
        //下载的完全路径
        File file = new File(path+filename);
        //转格式
        String name = new String(filename.getBytes("utf-8"),"iso8859-1");
        //头部信息 ,转流
        HttpHeaders hh = new HttpHeaders();
        hh.setContentDispositionFormData("attachment",name);
        hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),hh, HttpStatus.CREATED);

    }
}

Guess you like

Origin blog.csdn.net/weixin_54255580/article/details/123214768