81,092
社区成员
发帖
与我相关
我的任务
分享
@RequestMapping("upload")
@ResponseBody
public String uploadPreviewImage(HttpServletRequest request, HttpServletResponse response) throws IOException{
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
MultipartFile image = multipartRequest.getFile("advImage");
//临时图片路径
String dirPath = TEMP_PICTURE_PATH;
File filePath = new File(dirPath);
if (!filePath.exists()) {
filePath.mkdirs();
}
// 组合临时图片名
String imageName = image.getOriginalFilename();
String file_ext = imageName.substring(imageName.lastIndexOf(".") + 1);
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
String tempImageName = UNDERLINE + df.format(new Date()) + "." + file_ext;
// 存放浏览图片的零时文件路径
File file = new File(request.getSession().getServletContext().getRealPath(dirPath +
tempImageName));
byte[] advImageBytes = null;
InputStream advImageStream = null;
FileOutputStream os = null;
try {
/*file.createNewFile();
advImageStream = image.getInputStream();
advImageBytes = FileCopyUtils.copyToByteArray(advImageStream);
FileCopyUtils.copy(advImageBytes, file);
advImageStream.close(); */
advImageStream = image.getInputStream();
os=new FileOutputStream(file);
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while((len=advImageStream.read(buffer))>0){
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
os.write(buffer, 0, len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭输入流
advImageStream.close();
//关闭输出流
os.close();
}
String tempPath = dirPath +
tempImageName;
// String s = "{"+"\"tempPath\":\""+tempPath+"\","+"\"msg\":\"\""+"}";
// 传给页面相对路径
/*out.print("{");
out.print("\"tempPath\":\""+tempPath+"\",");
out.print("\"msg\":\"\"");
out.print("}");
out.flush();
out.close(); */
// ajax
Map<String, Object> result = new HashMap<String,Object>();
result.put("tempPath", tempPath);
result.put("msg", "");
return JSON.toJSONString(result);
}
这是java的
$.ajaxFileUpload({url:"upload.do",
secureuri:false,
fileElementId:"advImage",
dataType:"json",
success:function (data , status) {
returnData = data;
// 这个方法是现实一个div,托住截图框
$("#cutImageDiv").show();
// 一些变量在页面的隐藏input的设置
location = ctx_+returnData.tempPath;
$("#photo").attr('src',ctx_+returnData.tempPath+'');
$("#currentPath").val(returnData.tempPath);
},
error:function (data, status, e) {
//alert("图片上传失败,请重新选择图片");
}
});
这是js的 // 组合临时图片名
String imageName = image.getOriginalFilename();
String file_ext = imageName.substring(imageName.lastIndexOf(".") + 1);
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
String tempImageName = UNDERLINE + df.format(new Date()) + "." + file_ext;
// 存放浏览图片的零时文件路径
File file = new File(request.getSession().getServletContext().getRealPath(dirPath +
tempImageName));
byte[] advImageBytes = null;
InputStream advImageStream = null;
try {
file.createNewFile();
advImageStream = image.getInputStream();
advImageBytes = FileCopyUtils.copyToByteArray(advImageStream);
FileCopyUtils.copy(advImageBytes, file);
advImageStream.close();
} catch (IOException e) {
e.printStackTrace();
}
上传方法应该没问题吧,毕竟图片确实已经存放在目录里了,只是第一时间加载不出来,因为之前一直用的ftp上传,这次写的一个测试demo用到临时文件,就把放在服务器目录中了