ajaxfileupload上传图片抱java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
使用springmvc以及spring的标签,jquery1.7.1
尝试过在表单添加enctype="multipart/form-data",但这样在提交表单时就传不了参数页面表单<form:form id="inputForm" modelAttribute="clazz" action="${ctx}/sys/clazz/save" method="post">
<input type="hidden" name="id" value="${clazz.id}"/>
<fieldset class="prepend-top">
<legend>班级管理</legend>
<div id="messageBox" class="error" style="display:none">输入有误,请先更正。</div>
<table>
<tr>
<td><label for="name" class="field">班级名称:</label></td>
<td><input id="name" name="name" type="text" value="${clazz.name}" class="required"/></td>
</tr>
<tr>
<td><label for="description" class="field">班级简介:</label></td>
<td><textarea id="description" name="description" style="width:550px;height:45px;" >${clazz.description}</textarea></td>
</tr>
<tr>
<td><label for="culture" class="field">班级文化:</label></td>
<td><textarea id="culture" name="culture" style="width:550px;height:45px;">${clazz.description}</textarea></td>
</tr>
<tr>
<td><label for="teacherWords" class="field">教师心语:</label></td>
<td><textarea id="teacherWords" name="teacherWords" style="width:550px;height:45px;">${clazz.description}</textarea></td>
</tr>
<tr>
<td><label for="name" class="field">班级图片:</label></td>
<td>
<img id="imgUrl" src="${clazz.iconUrl}" width="80" height="60" />
<input type="hidden" value="${clazz.iconUrl}" id="iconUrl" name="iconUrl" />
<input id="filePath" type="file" />
<span style="color:red;font-size:8;">只能上传jpg、png、gif格式的图片</span>
<input type="button" value="上 传 " onclick="uploadPhoto();" />
</td>
</tr>
</table>
</fieldset>
<div>
<input id="submit" class="button" type="submit" value="提 交"/>
<input id="cancel" class="button" type="button" value="返 回" onclick="history.back()"/>
</div>
</form:form>
JS代码function uploadPhoto() {
var filePath = $("#filePath").val();
if(filePath == null) {
alert("请先选择文件再上传!");
return;
}
var suffix = filePath.substring(filePath.length-3, filePath.length);
if(suffix != "jpg" && suffix != "png" && suffix != "gif") {
alert("请上传jpg、png、gif格式的图片!");
return;
}
$.ajaxFileUpload( {
url : context + "/sys/upload",
secureuri : false,
fileElementId : "filePath",
dataType : "json",
success : function(data, status) {
var url = context + "/upload/" + data.result + "." +suffix;
$("#imgUrl").attr('src', url);
$("#iconUrl").attr('value', url);
},
error : function(data, status, e) {
alert(" upload error: " + data + " Status: " + status + " e: " + e);
}
});
}
后台java代码@Controller
@RequestMapping("/sys")
public class UploadController { @RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String create(RedirectAttributes redirectAttributes, HttpServletRequest request) { String userPath = request.getSession().getServletContext().getRealPath("/") + "/upload";
System.out.println("userUploadPath: " + userPath);
File attachPath = new File(userPath);
if(!attachPath.exists()) {
attachPath.mkdir();
}
UploadHelper helper = new UploadHelper();
helper.setTmpPath(userPath);
helper.setSavePath(userPath);
List<String> resultList = new ArrayList<String>();
String result = "";
Map<String,String> map = new HashMap<String,String>();
try {
resultList = helper.handleUplodFile(request);
map.put("result", resultList.get(0));
result = "{\""+ "result" + "\":\"" + resultList.get(0) + "\"}";
System.out.println("result: " + result);
} catch (FileUploadException e) {
map.put("result", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
map.put("result", e.getMessage());
e.printStackTrace();
}
return result;
}
}--------------------------------------------------------------------
各位大哥大姐,又遇到过类似的没,帮忙一下咯,在此先谢过了
ajaxfileupload数组越界
使用springmvc以及spring的标签,jquery1.7.1
尝试过在表单添加enctype="multipart/form-data",但这样在提交表单时就传不了参数页面表单<form:form id="inputForm" modelAttribute="clazz" action="${ctx}/sys/clazz/save" method="post">
<input type="hidden" name="id" value="${clazz.id}"/>
<fieldset class="prepend-top">
<legend>班级管理</legend>
<div id="messageBox" class="error" style="display:none">输入有误,请先更正。</div>
<table>
<tr>
<td><label for="name" class="field">班级名称:</label></td>
<td><input id="name" name="name" type="text" value="${clazz.name}" class="required"/></td>
</tr>
<tr>
<td><label for="description" class="field">班级简介:</label></td>
<td><textarea id="description" name="description" style="width:550px;height:45px;" >${clazz.description}</textarea></td>
</tr>
<tr>
<td><label for="culture" class="field">班级文化:</label></td>
<td><textarea id="culture" name="culture" style="width:550px;height:45px;">${clazz.description}</textarea></td>
</tr>
<tr>
<td><label for="teacherWords" class="field">教师心语:</label></td>
<td><textarea id="teacherWords" name="teacherWords" style="width:550px;height:45px;">${clazz.description}</textarea></td>
</tr>
<tr>
<td><label for="name" class="field">班级图片:</label></td>
<td>
<img id="imgUrl" src="${clazz.iconUrl}" width="80" height="60" />
<input type="hidden" value="${clazz.iconUrl}" id="iconUrl" name="iconUrl" />
<input id="filePath" type="file" />
<span style="color:red;font-size:8;">只能上传jpg、png、gif格式的图片</span>
<input type="button" value="上 传 " onclick="uploadPhoto();" />
</td>
</tr>
</table>
</fieldset>
<div>
<input id="submit" class="button" type="submit" value="提 交"/>
<input id="cancel" class="button" type="button" value="返 回" onclick="history.back()"/>
</div>
</form:form>
JS代码function uploadPhoto() {
var filePath = $("#filePath").val();
if(filePath == null) {
alert("请先选择文件再上传!");
return;
}
var suffix = filePath.substring(filePath.length-3, filePath.length);
if(suffix != "jpg" && suffix != "png" && suffix != "gif") {
alert("请上传jpg、png、gif格式的图片!");
return;
}
$.ajaxFileUpload( {
url : context + "/sys/upload",
secureuri : false,
fileElementId : "filePath",
dataType : "json",
success : function(data, status) {
var url = context + "/upload/" + data.result + "." +suffix;
$("#imgUrl").attr('src', url);
$("#iconUrl").attr('value', url);
},
error : function(data, status, e) {
alert(" upload error: " + data + " Status: " + status + " e: " + e);
}
});
}
后台java代码@Controller
@RequestMapping("/sys")
public class UploadController { @RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public String create(RedirectAttributes redirectAttributes, HttpServletRequest request) { String userPath = request.getSession().getServletContext().getRealPath("/") + "/upload";
System.out.println("userUploadPath: " + userPath);
File attachPath = new File(userPath);
if(!attachPath.exists()) {
attachPath.mkdir();
}
UploadHelper helper = new UploadHelper();
helper.setTmpPath(userPath);
helper.setSavePath(userPath);
List<String> resultList = new ArrayList<String>();
String result = "";
Map<String,String> map = new HashMap<String,String>();
try {
resultList = helper.handleUplodFile(request);
map.put("result", resultList.get(0));
result = "{\""+ "result" + "\":\"" + resultList.get(0) + "\"}";
System.out.println("result: " + result);
} catch (FileUploadException e) {
map.put("result", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
map.put("result", e.getMessage());
e.printStackTrace();
}
return result;
}
}--------------------------------------------------------------------
各位大哥大姐,又遇到过类似的没,帮忙一下咯,在此先谢过了
ajaxfileupload数组越界
解决方案 »
- 格式转换
- JS菜鸟问题,帮帮小弟!
- 如何在不刷新和不提交的前提下实现给某个文本框赋值?
- 怎么样取得一组checkbox的个数?
- 各位大哥大姐,帮帮忙.传值问题!在线等!
- 怎么做到页面提交一次就在页面下方表格中增加一行?非常感谢
- 撒分!在智能手机或移动设备上实现系列功能技术以及时间问题(功能:扫描条形码、在本地储存数据、在有信号或与电脑连接时连接某网站上传数据,判断是否上传成功)
- 高手们,请帮我看看该怎么写才对
- 日期自动增加的问题(急急急急急急急急,帮忙解决了高分相送!!!!!)
- 我以前找到一个和资源管理器一摸一样的树形目录的javascript代码,但是...
- 获取当前浏览器url的问题
- 有没有实现这种方式的曲线图?
private String tmpPath;
private String savePath;
/**
* @return the tmpPath
*/
public String getTmpPath() {
return tmpPath;
} /**
* @param tmpPath the tmpPath to set
*/
public void setTmpPath(String tmpPath) {
this.tmpPath = tmpPath;
} /**
* @return the savePath
*/
public String getSavePath() {
return savePath;
} /**
* @param savePath the savePath to set
*/
public void setSavePath(String savePath) {
this.savePath = savePath;
} public List<String> handleUplodFile(HttpServletRequest request) throws FileUploadException, IOException{
File tmpDir = new File(getTmpPath());
File saveDir = new File(getSavePath());
List<String> result = new ArrayList<String>();
if (!ServletFileUpload.isMultipartContent(request)) {
return result;
}
DiskFileItemFactory dff = new DiskFileItemFactory();
dff.setRepository(tmpDir);// 指定上传文件的临时目录
dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
FileItemIterator fii;
fii = sfu.getItemIterator(request);
while (fii.hasNext()) {
FileItemStream fis = fii.next();
if (fis.isFormField() || fis.getName().length() == 0)
continue;
String fileName = formatFileName(fis.getName());
BufferedInputStream in = new BufferedInputStream(fis.openStream());// 获得文件输入流
File file = new File(saveDir, fileName);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));// 获得文件输出流
Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
result.add(fileName);
}
return result;
}
private String formatFileName(String fileName){
return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + fileName.substring(fileName.lastIndexOf("."));
//return fileName;
}
}
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at cn.qtone.yjy.web.UploadController.upload(UploadController.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
if (isMultipart) {
try {
FileItemFactory factory = (FileItemFactory) new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> itr = items.iterator();
while (itr.hasNext()) {// 依次处理每个文件
FileItem item = (FileItem) itr.next();
File savedFile = ......;
item.write(savedFile);
}
......