写了个上传图片的功能,提交完form后,给我显示请求上传接口出现异常.但图片已经上传成功了.,
@PostMapping("/upload")
public Map<String, Object> upload(@RequestParam(value = "files")MultipartFile[] files,
GoodsVo goodsVo,
HttpServletRequest request) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
Map<String, Object> map = new HashMap<>();
try {
//上传了图片
for (MultipartFile file:files) {
// 获得文件类型
String fileType = file.getContentType();
if ("image/jpeg".equals(fileType) ||"image/jpg".equals(fileType) ||"image/png".equals(fileType)) {
// 原名称
String oldFileName = file.getOriginalFilename();
// 获得文件后缀名称
String imageName = fileType.substring(fileType.indexOf("/") + 1);
// 新名称
String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + imageName;
String path = "/D:/image";
String realPath = path.replace('/', '\\').substring(1, path.length());
File f = new File(realPath, newFileName);
// 完成文件的上传
file.transferTo(f);
String path01 = "/image/" + newFileName;
stringBuilder.append(path01+",");
goodsVo.setGoodsimg(stringBuilder.toString());
map.put("msg","ok");
map.put("code",200);
}
}
goodsService.save(goodsVo);
}catch (Exception e){
e.printStackTrace();
map.put("msg","error");
map.put("code",0);
}
return map;
}
form.on("submit(doSubmit)",function(data){
console.log(data)
var form = new FormData();
for (let i in files){
form.append("files",files[i]);
}
form.append('goodsname',data.field.goodsname);
form.append('produceplace',data.field.produceplace);
form.append('size',data.field.size);
form.append('goodspackage',data.field.goodspackage);
form.append('productcode',data.field.productcode);
form.append('promitcode',data.field.promitcode);
form.append('description',data.field.description);
form.append('price',data.field.price);
form.append('number',data.field.number);
form.append('dangernum',data.field.dangernum);
form.append('goodsimg',data.field.goodsimg);
form.append('available',data.field.available);
form.append('providerid',data.field.providerid);
console.log(data.field)
$.ajax({
url:'/goods/upload',
type: "post",
dataType: "json",
async: false,
contentType: false,
processData: false,
data: form,
success:function (result) {
console.log(result)
if (result.code==200){
layer.msg(result.msg);
tableIns.reload();
layer.close(mainIndex);
return false;
}
},
error:function (res) {
layer.close("网络错误,请检查!");
}
});
});
//多图容器
upload.render({
elem: '#btn_multiple_upload_img'//绑定点击按钮
,multiple: true//确认上传多张图片
,auto:false//取消自动上传
,accept: 'images'//图片格式
,bindAction:'#doSubmit'//指定按钮上传
,acceptMime: 'image/*'
,method:'post'
,choose:function (obj) {
//预读本地文件示例,不支持ie8
obj.preview(function(index, file, result){
$('#upload_image_list').append('<img style="height: 66px;margin-left: 7px" src="'+ result +'" title="单击删除" onclick="delMultipleImgs(this)" class="layui-upload-img">');
});
files = obj.pushFile(); //将每次选择的文件追加到文件队列
}
,done: function(res){
console.log(res)
}
});
上面是我的代码,有没有老哥知道是什么问题? 我后端使用的是@RestController注解