#单个数据的大小
multipart.maxFileSize=10Mb
#总数据的大小
multipart.maxRequestSize=10Mb
#文件上传目录(window是d/e/f盘, linux是/)(注意Linux和Windows上的目录结构不同)
linux.file.uploadFolder=/
window.file.uploadFolder=E://yema//
# 设备截图文件保存路径(文件存在具体的文件夹的路径)
smas.captrue.image.path=upload/images
#浏览器访问
file.uri=/upload/
// 设备截图文件保存路径(文件存在具体的文件夹的路径)
@Value("${smas.captrue.image.path}")
private String captureImagePath;
// 文件上传目录(window是d/e/f盘, linux是/)
@Value("${window.file.uploadFolder}")
private String winUploadFolder;
// 文件上传目录(window是d/e/f盘, linux是/)
@Value("${linux.file.uploadFolder}")
private String linuxUploadFolder;
// #浏览器访问
@Value("${file.uri}")
private String uri;
/**
* 保存图片
*
* @return ResultMsg<?>
* @author xiaoli
* @date 2019年9月19日下午4:04:13
*/
@PostMapping("/savePhoto")
public ResultMsg<?> savePhoto(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
DishesInfoEntity dishesInfoEntity = new DishesInfoEntity();
try {
String fileName = file.getOriginalFilename(); //文件名
// 后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 上传后图片保存的路径
String path_deposit;
// 判断系统
if (FileUtils.getRootPathByOS()) {
path_deposit = linuxUploadFolder + captureImagePath;
} else {
path_deposit = winUploadFolder + captureImagePath;
}
// 新文件名
fileName = Commons.getUUID() + suffixName;
FileUtils.getFileByBytes(file.getBytes(), path_deposit, fileName);
// 文件转换为字节数组
dishesInfoEntity.setFDishID(Integer.valueOf(request.getParameter("fDishID")));
dishesInfoEntity.setFPicture(file.getBytes());
dishesInfoEntity.setFPhotoCachePath(FileUtils.getServerIPPort(request) + File.separator + uri + fileName);
//图片在浏览器访问路径
System.out.println(FileUtils.getServerIPPort(request) + File.separator + uri + fileName);
dishesInfoService.saveOrRefreshPicture(dishesInfoEntity);
// 文件上传
} catch (Exception e) {
e.printStackTrace();
return ResultUtils.returnError("保存失败。。。");
}
return ResultUtils.returnSuccess();
}
FilterUtils工具类
package com.yema.commons;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 文件帮助类
* @Author:
* @Date : 2019年9月19日上午11:48:59
*/
public class FileUtils {
/**
* 复制文件并输入文件
* @Author:
* @Date : 2019年9月19日上午11:42:48
* @param : @param filePath1 原文件
* @param : @param filePath2 复制之后的文件
*/
public static void copyFile(String filePath1, String filePath2) {
try {
// 读取本地文件
FileInputStream in = new FileInputStream(filePath1);
// 创建新的输出文件类
FileOutputStream out = new FileOutputStream(filePath2);
byte b[] = new byte[1024];
while (in.read(b) > -1) {
out.write(b);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件转成byte[]
* @Author:
* @Date : 2019年9月19日上午11:37:37
* @param : @param filePath
* @param : @return
* @return: byte[]
*/
public static byte[] getBytesByFile(String filePath) {
// 创建文件类
File file = new File(filePath);
try {
FileInputStream fis = new FileInputStream(file);
// 字节输出流,其中数据被写入到字节数组中
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
byte[] data = bos.toByteArray();
bos.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 文件转成byte[]
* @Author:
* @Date : 2019年9月19日上午11:44:09
* @param : @param file
* @param : @return
* @return: byte[]
*/
public static byte[] getBytesByFile(File file) {
try {
FileInputStream fis = new FileInputStream(file);
// 字节输出流,其中数据被写入到字节数组中
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
byte[] data = bos.toByteArray();
bos.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将byte数组转换成文件
* @Author:
* @Date : 2019年9月19日下午12:02:42
* @param : @param bytes
* @param : @param filePath
* @param : @param fileName
*/
public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {
// 字节缓冲输出流
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
// 判断文件目录是否存在
if (!dir.exists() ) {
dir.mkdirs();
}
file = new File(filePath + "\\" + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 把某个文件写到指定地址
* @Author:
* @Date : 2019年9月19日上午11:51:21
* @param : @param file 文件
* @param : @param filePath 写入地址
*/
public static void writeFile(File file, String filePath) {
FileOutputStream outputStream;
byte b[] = getBytesByFile(file);
try {
outputStream = new FileOutputStream(filePath);
outputStream.write(b);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取网络图片流
* @author :
* @create : 2019/11/1 11:39
**/
public static InputStream getImageStream(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
return inputStream;
}
} catch (IOException e) {
System.out.println("获取网络图片出现异常,图片路径为:" + url);
e.printStackTrace();
}
return null;
}
/**
* 删除文件
*
* @param path 文件访问的路径upload开始 如: /upload/image/test.jpg
* @return true 删除成功; false 删除失败
*/
public static boolean delete(String path) {
File file = new File(path);
return file.exists() == true ? file.delete() : true;
}
//获取根目录
public static Boolean getRootPathByOS(){
String osName = System.getProperties().getProperty("os.name");
System.out.println(osName);
if(osName != null && osName.toLowerCase().contains("linux")){
return true;
}else{
return false;
}
}
/**
* 获取服务部署根路径 http:// + ip + port
*
* @param request
* @return
*/
public static String getServerIPPort(HttpServletRequest request) {
return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
}
}
tomcat配置文件server.xml
docBase ="图片的真实放置的路径"
path="外网访问的路径"
<!--window-->
<Context docBase ="E:\yema\upload\images" path ="/upload" debug ="0" reloadable ="true"/>
<!--linux-->
<Context docBase ="/upload/images" path ="/upload" debug ="0" reloadable ="true"/>
页面样式 另外一篇博客:https://blog.csdn.net/qq_44045573/article/details/104967754
个人踩坑记录一下:在本地的图片上传无法访问,tomcat配置文件要配置