诶诶诶略 2016-08-25 07:16 采纳率: 0%
浏览 1708

sprig mvc+ajaxFileUpload实现多文件上传,为什么只能上传第一张




//////////////////////////////////////////////////////////////////////////////////
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;
}

  • 写回答

2条回答 默认 最新

  • dabocaiqq 2017-02-19 15:54
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏