java路径配置与映射
前言
我是刚学习时间不长的小白,java基础案例里面第八章有关于文件流的介绍,仅仅限于windows系统的本地上传与下载,这是我学习spring boot项目配置的一个文件上传和访问路径。
一、文件上传的配置
1、这是Linux系统上传文件的路径配置。
FILE_BASE_PATH : Linux系统文件上传路径
PIC_TYPE : “doc” 上传文件工具类中创建的文件夹名称
HOST_PORT_URL : 存储到服务器中的路径(返回路径)
booking:服务器的名称,这个必须得写否则会报404
2、这是windows系统上传文件的路径配置。
二、前端访问的映射路径
1.Linux系统前端页面访问服务器的映射路径配置
如图:
file:表示上传的文件路径
2.window系统前端页面访问服务器的映射路径配置
如图:
总结
这里对文章进行总结:
文件配置的上传路径和映射路径必须完全一致,否则会报错
#此处分享文件上传的代码
controller层类
/**
*
* @version: 1.1.0
* @Description: 文件上传
* @author: yrb
* @date: 2020年8月09日上午10:34:10
*/
@RequestMapping(value="/Member/FirstFile/uploadFiles",method=RequestMethod.POST)
public @ResponseBody R uploadFiles(@RequestParam("file") MultipartFile [] files) {
try {
Integer index=1;
List<String> path=FileTools.uploadFile(files, FileConstant.PIC_TYPE[index]);
List<String> picPath = path.stream().map(x ->FileConstant.HOST_PORT_URL+x).collect(Collectors.toList());;
return R.ok().put("picPath",picPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return R.error("文件上传失败请重试");
// return "Unable to upload. File is empty.";
}
}
存放静态数据的类
public class FileConstant {
public static final String FILE_BASE_PATH="/root/booking-management/file/"; // Linux系统文件上传路径
public static final String[] PIC_TYPE= {"picture","doc","mechanismIcon"};
public static final String register_code="";
public static final String HOST_PORT_URL="http://39.106.166.147:8081/booking/";
}
文件上传工具类
package sjz.clkj.booking.management.util;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import sjz.clkj.booking.management.constant.FileConstant;
public class FileTools {
/**
* 多文件上传
* @param file
* @param type
* @return
* @throws IOException
*/
public static List<String> uploadFile(MultipartFile file[], String type) throws IOException {
List<String> picDataPathList = new ArrayList<>();
String extName = "";
String filePath = FileConstant.FILE_BASE_PATH + type;
if (file != null && file.length >= 1) {
for (MultipartFile multipartFile : file) {
String originalFilename = multipartFile.getOriginalFilename();// 得到上传的文件名
extName = originalFilename.substring(originalFilename.lastIndexOf("."));// 得到图片的后缀
// 为了不让图片不重复进行相关操作
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String nowTime = sdf.format(new Date());
int randomCode = (int) (Math.random() * (999 - 100) + 100);
// 生成新文件名
String fileName = nowTime + randomCode + extName;
// String fileName = originalFilename + extName;
// 创建了一个File对象,名字是newFile,路径是filePath,名字是filename。
File newFile = new File(filePath, fileName);
if (!newFile.exists()) {
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
newFile.createNewFile();
}
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), newFile);
String picDataPath = type + "/" + fileName;
picDataPathList.add(picDataPath);
}
}
return picDataPathList;
}
/**
* 单文件上传
* @param file
* @param type
* @return
* @throws IOException
*/
public static String uploadFile(MultipartFile file, String type) throws IOException {
String extName = "";
String filePath = FileConstant.FILE_BASE_PATH + type;
String originalFilename = file.getOriginalFilename();// 得到上传的文件名
extName = originalFilename.substring(originalFilename.lastIndexOf("."));// 得到图片的后缀
// 为了不让图片不重复进行相关操作
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String nowTime = sdf.format(new Date());
int randomCode = (int) (Math.random() * (999 - 100) + 100);
// 生成新文件名
String fileName = nowTime + randomCode + extName;
// 创建了一个File对象,名字是newFile,路径是filePath,名字是filename。
File newFile = new File(filePath, fileName);
if (!newFile.exists()) {
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
newFile.createNewFile();
}
FileUtils.copyInputStreamToFile(file.getInputStream(), newFile);
String picDataPath = type + "/" + fileName;
return picDataPath;
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
}