问题描述
- springmvc+easyui实现图片上传
- @RequestMapping(value=""/upload""method=RequestMethod.POST)
@ResponseBody
public Json upload(@RequestParam(value=""imageFile"") MultipartFile fileHttpServletRequest reqHttpServletResponse resp) throws IOException{
String temppath=""/image"";
Json j = new Json();
//构建文件名称
String oldName = file.getOriginalFilename();
//获取文件的后缀
String suffix = oldName.substring(oldName.indexOf('.'));
String name = String.valueOf(System.currentTimeMillis()).concat(suffix);
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;
}<tr> <td>法人身份证照片</td> <td><input type=""file"" name=""imageFile"" id=""imageFile"" onchange=""previewImage(this)"" accept=""image/*""></td> <td> <input type=""button"" onclick=""return ajaxFileUpload();"" value=""上传""/> <input type=""hidden"" name=""legal_url"" id=""legal_url""/> </td> </tr> <tr> <td>营业执照照片</td> <td><input type=""file"" name=""imageFile"" id=""imageFile""></td> <td> <input type=""button"" onclick=""return ajaxFileUpload1();"" value=""上传""/> <input type=""hidden"" name=""busi_url"" id=""busi_url""/> </td> </tr> <tr> <td>公司前台图片</td> <td><input type=""file"" name=""imageFile"" id=""imageFile""></td> <td> <input type=""button"" onclick=""return ajaxFileUpload2();"" value=""上传""/> <input type=""hidden"" name=""comp_url"" id=""comp_url""/> </td> </tr> //法人身份证照片function ajaxFileUpload() { $.ajaxFileUpload({ url : '${pageContext.request.contextPath}/commonController/upload' type : 'post' secureuri:false fileElementId : 'imageFile' dataType : 'JSON' success : function(data status) { //去掉<pre></pre>标签 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) { $(""#legal_url"").val(data.errorinfo); alert(""上传成功!""); } } }); return false;}//营业执照照片function ajaxFileUpload1() { $.ajaxFileUpload({ url : '${pageContext.request.contextPath}/commonController/upload' type : 'post' secureuri:false fileElementId : 'imageFile' dataType : 'JSON' success : function(data status) { //去掉<pre></pre>标签 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) { $(""#busi_url"").val(data.errorinfo); alert(""上传成功!""); } } }); return false;}//公司前台照片function ajaxFileUpload2() { $.ajaxFileUpload({ url : '${pageContext.request.contextPath}/commonController/upload' type : 'post' secureuri:false fileElementId : 'imageFile' dataType : 'JSON' success : function(data status) { //去掉<pre></pre>标签 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) { $(""#comp_url"").val(data.errorinfo); alert(""上传成功!""); } } }); return false;}
解决方案
上传第一个没问题,第三个上传的图片跟第一个一样,第二个无图片
解决方案二:
你把
<input type=""file"" name=""imageFile"" id=""imageFile"">
这里面的id和name换成不一样试试看
解决方案三:
SpringMVC实现angularjs图片上传
时间: 2024-09-08 08:37:48