//////////////////////////////////////////////////////////////////////////////////
function ajaxFileUpload() {
$.ajaxFileUpload({
url : '${pageContext.request.contextPath}/commonController/fileUpload',
type : 'post',
secureuri:false,
fileElementId : ['imageFile','imageFile1','imageFile2'],
dataType : 'JSON',
success : function(data, status) {
//去掉
var start = data.indexOf(">");
if(start != -1) {
var end = data.indexOf("<", start + 1);
if(end != -1) {
data = data.substring(start + 1, end);
}
}
//转化为JSON对象
data=eval('('+data+')');
if (data.success) {
$("#le_url").val(data.errorinfo);
$("#bu_url").val(data.errorinfo);
$("#co_url").val(data.errorinfo);
alert(data.errorinfo);
$.messager.alert('<spring:message code="hint"/>', data.msg, 'info');
$("#showPic").attr("src",data.errorinfo);
$("#showPic").show();
$("#showPic1").show();
$("#showPic2").show();
}else{
$.messager.alert('<spring:message code="hint"/>', data.msg, 'info');
}
},
});
return false;
}
////////////////////////////////////////////////////////////////////////////////
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
@ResponseBody
public Json upload(@RequestParam(value="imageFile")MultipartFile[] files,
HttpServletRequest req) throws IOException {
String temppath = "/image";
Json j = new Json();
// 构建文件名称multipartFile
for (MultipartFile file : files) {
String oldName = file.getOriginalFilename();
// 获取文件的后缀
String suffix = oldName.substring(oldName.indexOf('.'));
//String name = String.valueOf(System.currentTimeMillis()).concat(suffix);
String name = oldName;
String path = req.getSession().getServletContext()
.getRealPath(temppath);
try {
File filepath = new File(path);
if (!filepath.exists()) {
filepath.mkdir(); // 创建目录
}
OutputStream os = new FileOutputStream(filepath.getPath() + "/"
+ name);
InputStream is = file.getInputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
j.setSuccess(true);
j.setMsg("上传成功");
} catch (Exception e) {
j.setMsg("上传失败");
j.setSuccess(false);
e.printStackTrace();
}
j.setErrorinfo(req.getContextPath() + temppath + "/" + name);
j.setObject(path + "\" + name);
}
return j;
}