JS
function subimtBtn() {
var formdata = new FormData(); //封装数据
var fileValue = KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲upload').val();…(’#upload’)[0].files[0]);
KaTeX parse error: Expected '}', got 'EOF' at end of input: … url:"{ctx}/ossc/label/label/upload",
type: “POST”,
async: false,
processData: false, // jQuery不要去处理发送的数据
contentType: false, // jQuery不要去设置Content-Type请求头
data:formdata,
success: function (data) {
console.log(data);
if(data.resultCode=“111”){
window.parent.alert(“上传成功”);
window.parent.location.reload();//关闭当前页面弹窗
}
},
error:function(data){
console.log(data);
window.parent.alert(“上传图片失败,请联系管理员”);
},
});
}
请选择一个图片上传:
上传
上传图片
@RequestMapping(value=“upload”, method=RequestMethod.POST)
@ResponseBody
public String upload( @RequestParam(“file”) MultipartFile file,HttpServletRequest request) {
System.out.println("file.getOriginalFilename() = " + file.getOriginalFilename());
String result = null;//上传结果信息
Map<String,Object> map=new HashMap<String, Object>();
if (file.getSize() / 1000 > 1000){
result="图片大小不能超过1000KB";
}
else {
//判断上传文件格式
String fileType = file.getContentType();
if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpg")) {
//获取文件名
String fileName = file.getOriginalFilename();
//获取文件后缀名
int index = fileName.lastIndexOf(".");
String suffixName;
if (index > 0)
suffixName = fileName.substring(fileName.lastIndexOf("."));
else
suffixName = ".png";
//重新生成文件名
String imgFilePrefix = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
fileName = imgFilePrefix + suffixName;
//设置保存路径
String projectPath = "D:\\data\\curve";
byte [] byteArr= new byte[0];
try {
byteArr = file.getBytes();
FileUtils.writeByteArrayToFile(new File(projectPath, fileName), byteArr);
map.put("resultCode", "111");
map.put("resultMsg", "上传图片成功");
} catch (IOException e) {
e.printStackTrace();
map.put("resultCode", "999");
map.put("resultMsg", "上传图片失败,请联系管理员");
}
}
}
result = JSONObject.toJSONString(map);
return result;
}