这个是报错的信息
这个是我的目录信息
这个是我的文件跳转
package com.controller;
import cn.hutool.core.date.DateUtil;
import com.common.AppFileUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("file")
public class FileController {
/**
* 文件上传
* @param mf
* @return
*/
@RequestMapping("uploadFile")
public Map<String,Object> uploadFile(MultipartFile mf) {
//1.得到文件名
String oldName = mf.getOriginalFilename();
//2.根据旧的文件名生成新的文件名
String newName= AppFileUtils.createNewFileName(oldName);
//3.得到当前日期的字符串
String dirName= DateUtil.format(new Date(), "yyyy-MM-dd");
//4.构造文件夹
File dirFile=new File(AppFileUtils.UPLOAD_PATH,dirName);
//5.判断当前文件夹是否存在
if(!dirFile.exists()) {
//如果不存在则创建新文件夹
dirFile.mkdirs();
}
//6.构造文件对象
File file=new File(dirFile, newName+"_temp");
//7.把mf里面的图片信息写入file
try {
mf.transferTo(file);
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
Map<String,Object> map=new HashMap<String, Object>();
map.put("path",dirName+"/"+newName+"_temp");
return map;
}
/**
* 图片下载
*/
@RequestMapping("showImageByPath")
public ResponseEntity<Object> showImageByPath(String path){
return AppFileUtils.createResponseEntity(path);
}
}
这个是我设置的功能
package com.common;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class AppFileUtils {
/**
* 文件上传的保存路径 默认值
*/
public static String UPLOAD_PATH="D:/upload/";
static {
//通过反射的方式,读取配置文件的存储地址
InputStream stream = AppFileUtils.class.getClassLoader().getResourceAsStream("file.properties");
Properties properties=new Properties();
try {
properties.load(stream);
} catch (IOException e) {
e.printStackTrace();
}
String property = properties.getProperty("filepath");
if(null!=property) {
UPLOAD_PATH=property;
}
}
/**
* 根据文件老名字得到新名字
* @param oldName 文件老名字
* @return 新名字由32位随机数加图片后缀组成
*/
public static String createNewFileName(String oldName) {
//获取文件名后缀
String stuff=oldName.substring(oldName.lastIndexOf("."), oldName.length());
//将UUID与文件名后缀进行拼接,生成新的文件名 生成的UUID为32位
return IdUtil.simpleUUID().toUpperCase()+stuff;
}
/**
* 文件下载
* @param path
* @return
*/
public static ResponseEntity<Object> createResponseEntity(String path) {
//1,构造文件对象
File file=new File(UPLOAD_PATH, path);
if(file.exists()) {
//将下载的文件,封装byte[]
byte[] bytes=null;
try {
bytes = FileUtil.readBytes(file);
} catch (Exception e) {
e.printStackTrace();
}
//创建封装响应头信息的对象
HttpHeaders header=new HttpHeaders();
//封装响应内容类型(APPLICATION_OCTET_STREAM 响应的内容不限定)
header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//创建ResponseEntity对象
ResponseEntity<Object> entity= new ResponseEntity<Object>(bytes, header, HttpStatus.CREATED);
return entity;
}
return null;
}
/**
* 更该图片的名字 去掉_temp
* @param goodsImg
* @return
*/
public static String renameFile(String goodsImg) {
File file = new File(UPLOAD_PATH,goodsImg);
String replace = goodsImg.replace("_temp","");
if (file.exists()){
file.renameTo(new File(UPLOAD_PATH,replace));
}
return replace;
}
/**
* 根据老路径删除图片
* @param oldPath
*/
public static void removeFileByPath(String oldPath) {
//图片的路径不是默认图片的路径
String DEFAULT_IMG_GOODS = "/images/zwtp.png";
if (!oldPath.equals(DEFAULT_IMG_GOODS)){
File file = new File(UPLOAD_PATH,oldPath);
if (file.exists()){
file.delete();
}
}
}
}
请问这是怎么引起的
请求"showImageByPath"接口没传具体的文件名,接口内应先检验path合法性,工具类那边应该再判断下file不是文件夹