在springMVC中使用ajax进行上传图片后一直返回error
在做一个图片的异步上传的时候,我可以将图片保存到本地但是我想再返回这个本地图片的路径的时候,ajax一直进error,就是返回不出来
js方法
function ajaxFileUpload() {
$.ajaxFileUpload({
url : "${pageContext.request.contextPath}/test/repairs/repairssave.action",
secureuri : false,
fileElementId : 'file',
dataType : 'json',
data : {file : $("#file").val()},
success : function(data, status) {
alert("success");
alert('上传成功'+data.project);
},
error : function(data,status,e) {
alert('上传失败'+data.project);
}
})
return true;
}
body里边的内容
<td>
<br/>
1
<c:out value="${AA}"></c:out>
1
<br/>
<input type="text" name="picture" value="${project}">
上传文件1:<input type="file" name="file" id="file"/>
<input type="button" value="上传图片" onclick="ajaxFileUpload();"/>
</td>
controlle层
@RequestMapping("test/repairs/repairssave")
public @ResponseBody Map<String, String> upload(@RequestParam("file") MultipartFile[] files,HttpServletRequest request,HttpServletResponse response,Model model) throws IOException{
request.setCharacterEncoding("utf-8"); // 设置编码
response.setCharacterEncoding("utf-8");
//创建用来保存文件地址的lisi集合
List<String> fileUilList=new ArrayList<String>();
Map<String, String> map=new HashMap<String, String>();
String path="";
//判断files是否为空
if(files!= null && files.length>0){
//循环遍历,取出单个文件
for (MultipartFile file : files) {
//判断file是否为空,必须要有
if (!file.isEmpty()) {
//获取原始数据名
String fileName=file.getOriginalFilename();
//获取新数据名
String newFileName=new Date().getTime()+String.valueOf(fileName);
//获取物理路径websuo所在路径
String pathRoot = request.getSession().getServletContext().getRealPath("");
System.out.println(pathRoot );
//项目下的相对路径
path = FILE_PATH+newFileName;
//创建文件实例
File tempFile=new File(pathRoot+path);
if (!tempFile.getParentFile().exists()) { //这个判断必须加上
tempFile.getParentFile().mkdir();
}
if (!tempFile.exists()) {
tempFile.mkdir();
}
try {
//将接收到的文件传输到指定的目标文件
file.transferTo(tempFile);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//保存到list集合中
fileUilList.add(path);
map.put("project", path);
}
}
model.addAttribute("project",path);
}
/*HttpSession session=request.getSession();
session.setAttribute("project",path);*/
JSONObject json=JSONObject.fromObject(map);
response.getWriter().write(json.toString());
response.getWriter().close();
return map;
}
然后他就各种的返回不回去