问题遇到的现象和发生背景
如题,,用上传的新图片替换旧的,这个更新过程,如果上传的图片名含有中文,就会报错乱码,但Elemets里的图片url是正确的
问题相关代码,请勿粘贴截图
/**
* 更新歌手图片
*/
@RequestMapping(value = "/updateSingerPic",method = RequestMethod.POST)
public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile,@RequestParam("id")int id) throws UnsupportedEncodingException {
JSONObject jsonObject = new JSONObject();
if (avatorFile.isEmpty()){
jsonObject.put(Consts.CODE,0);
jsonObject.put(Consts.MSG,"文件上传失败");
return jsonObject;
}
//文件名=当前时间到毫秒+原来的文件名
String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
//文件路径
String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
+System.getProperty("file.separator")+"singerPic";
//如果文件不存在,新增该路径
File file1 = new File(filePath);
if(!file1.exists()){
file1.mkdir();
}
//实际的文件地址
File dest = new File(filePath+System.getProperty("file.separator")+fileName);
//存储到数据库里的相对文件地址
String storeAvatorPath = "/img/singerPic/"+fileName;
try {
avatorFile.transferTo(dest);
Singer singer = new Singer();
singer.setId(id);
singer.setPic(storeAvatorPath);
boolean flag = singerService.update(singer);
if (flag){
jsonObject.put(Consts.CODE,1);
jsonObject.put(Consts.MSG,"上传成功");
jsonObject.put("pic",storeAvatorPath);
return jsonObject;
}
jsonObject.put(Consts.CODE,0);
jsonObject.put(Consts.MSG,"上传失败");
return jsonObject;
} catch (IOException e) {
jsonObject.put(Consts.CODE,0);
jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());
}finally {
return jsonObject;
}
运行结果及报错内容
谷歌console报错
GET http://127.0.0.1:8888/img/singerPic/1642912341952%E5%8D%8E%E4%BB%94.jpg 404
我的解答思路和尝试过的方法
试过很多修改utf-8编码方式,都没啥用
我想要达到的结果
中文名图片也可以上传