springMVC文件的上传和下载

springMVC的文件上传和下载

package com.spring1;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@RestController
public class UploadHandler {
@RequestMapping("/hello")
public String hello(){
System.out.println(“hello”);
return “ok”;
}
@PostMapping("/upload")
public String upload(String cname, MultipartFile head, HttpServletRequest request,MultipartRequest req) throws IOException {
System.out.println(cname);
//获得上传文件名
String name = head.getOriginalFilename();
//获得上传目录的绝对路径
String path = request.getServletContext().getRealPath("/upload");
System.out.println(path);
File file = new File(path,name);
head.transferTo(file);
return “ok”;
}
@PostMapping("/upload2")
public String upload2(String cname, MultipartFile[] head, HttpServletRequest request){
System.out.println(cname);
for(MultipartFile mf : head){
System.out.println(mf.getOriginalFilename()+","+mf.isEmpty());
}
return “ok”;
}

@PostMapping("/upload3")
public String upload3(String cname, List<MultipartFile> head, HttpServletRequest request,MultipartRequest mr){
    System.out.println(cname);
    for(MultipartFile mf : head){
        System.out.println(mf.getOriginalFilename()+","+mf.isEmpty());
    }

    List<MultipartFile> head2=mr.getFiles("head");
    for(MultipartFile mf : head2){
        System.out.println(mf.getOriginalFilename()+","+mf.isEmpty());
    }

    return "ok";
}
@RequestMapping("/down")
public ResponseEntity<byte[]> down(String name,HttpServletRequest request) throws IOException {
    System.out.println(name);
    String path = request.getServletContext().getRealPath("/upload");

    File file = new File(path,name);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    byte[] data = new byte[in.available()];
    in.read(data);
    in.close();

    //设置响应头类型
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    //编码文件名,避免中文乱码
    name = new String(name.getBytes("utf-8"),"iso-8859-1");
    headers.setContentDispositionFormData("attachment",name);

    ResponseEntity<byte[]> entity = new ResponseEntity<>(data,headers, HttpStatus.OK);
    return entity;
}
@RequestMapping("/down2")
public String down2(String name, HttpServletRequest request, HttpServletResponse response) throws IOException{
    System.out.println(name);
    String path = request.getServletContext().getRealPath("/upload");

    File file = new File(path,name);

    //设置响应类型
    response.setContentType("application/octet-stream");
    //设置响应头
    response.setHeader("Content-disposition","attachment;filename="
            +new String(name.getBytes("utf-8"),"iso-8859-1"));
    response.setHeader("Content-length",file.length()+"");

    //输入流读文件
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    //输出流写到响应流中
    BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
    byte[] buffer = new byte[in.available()];
    in.read(buffer);
    out.write(buffer);

    out.flush();
    out.close();
    in.close();
    return "ok";
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值