文件上传
浏览器端上传一个字节文件(MultipartFile,他其实把上传的一个或多个文件(part)封装成了Map<String,MultipartFile>,在multipartFile的实现类里装了多个part.因为父类接口可以接实现类。所以请求参数可以设置成multipartFile)
服务器读
然后写到指定存储位置
使用mvc提供的multipartResolver文件解析器来做
请求方式必须是post
1.dispatcher中配置<multipart-config></multipart-config>
,设置允许接收content-type为multipart/form-data的文件数据(允许接字节)。
2.配置文件解析器
2.1.在springmvc.xml中配置文件解析器对象
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
或
<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"></bean>
2.2.bean 的id必须是multipartResolver
配置文件进容器
才能用注解读到
<context:property-placeholder location="classpath:mvc.properties" ignore-unresolvable="true"></context:property-placeholder>
package com.pb.controller.fileload;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import com.pb.day0905.exception.MvcException;
import com.pb.entity.AioxsMsg;
import com.pb.entity.E;
import com.pb.entity.Emp;
import com.pb.entity.MvcProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.lang.invoke.VarHandle;
import java.net.URLEncoder;
import java.util.*;
/**
* @author: pb
* @clasName: JAVASM
* @description:
* @date: 2022/9/5 22:06
* @version: 0.1
* @since: jdk11
*/
@RestController
public class Upload {
@PostMapping("upload")
public AioxsMsg doUpLoadFile(MultipartFile myFile) throws IOException {
//1.读文件
//2.写文件
// byte[] bytes = myFile.getBytes(); //文件内容字节数组。小文件
InputStream is = myFile.getInputStream();//文件内容数据流。大文件
long size = myFile.getSize();//文件大小
System.out.println("文件大小"+size);
String originalFilename = myFile.getOriginalFilename();//文件名,默认iso编码格式
System.out.println("文件名:"+originalFilename);
//设置本地存储路径(创建properties文件,将路径存储进去,再创建一个读取文件数据的类(@value(${})),再读这个类的属性)
String root = "D:/upload"; //本地磁盘路径
String timeDir = DateUtil.format(new Date(),"yyyyMMdd");//时间戳
//创建文件对象,路径是root+timedir
File f = new File(root,timeDir);
if(!f.exists())
f.mkdirs();//文件不存在就创一个路径
//总文件路径:磁盘路径+时间戳+原文件名
System.out.println(f.getPath()+"\\"+originalFilename);
FileOutputStream fos = new FileOutputStream(f.getPath()+"\\"+originalFilename);//将文件写入到这个路径
byte[] b = new byte[2048];
int read;
//将文件内容数据流加载到字节数组中,再交给输出流来读
while ((read = is.read(b))>0){
fos.write(b,0,read); //目标文件 开始 结束
}
fos.close();
//写完把文件信息回传到前端()
Map<String,Object> map = new HashMap<>();
return null;
}
@GetMapping("show")
public ResponseEntity showFile(String path) throws FileNotFoundException {
//1.先判断存储路径存不存在 :根+文件夹+原文件名
//2.不存在抛异常
//3.存在把文件放到输入流里给hutool工具读
//4.把字节数组放到respondEntity里
String root = MvcProperties.getRoot();
File f = new File(root,path);
if(!f.exists()){
throw new MvcException(E.NO_FILE);
}
byte[] bytes = IoUtil.readBytes(new FileInputStream(f), true);
return ResponseEntity.ok(bytes);
}
@GetMapping("download")
public ResponseEntity downLoad(String path) throws FileNotFoundException, UnsupportedEncodingException {
//上传时为了避免重名需要对文件名作处理,但下载时需要获得文件真实名字
//从浏览器获得服务器文件数据
String root = MvcProperties.getRoot();
File f = new File(root,path);
if(!f.exists()) throw new MvcException(E.NO_FILE);
//获得文件字节数组数据
//将这个数据塞进响应体返回出去
//获得真实文件名
//设置可以下载需要先设置响应头,在吧响应头页塞进响应体中
byte[] bytes = IoUtil.readBytes(new FileInputStream(f), true);
String realName = f.getName();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Disposition","attachment;filename="+ URLEncoder.encode(realName,"UTF-8"));
return new ResponseEntity(bytes,httpHeaders, HttpStatus.OK);
}
}