在使用jspsmartupload组件上传文件时,如果同时上传多个文件就会抛出该异常,在此做下记录
<fieldset>
<legend align="left">文件上传</legend>
<p>作者:<input type="text" id="author" name="author" /></p>
<p>出版社:<input type="text" id="press" name="press" /></p>
<p>上传文件:<input type="file" name="filename" /></p>
<p>上传文件:<input type="file" name="filename" /></p> <!-- 此处使用多文件上传会抛出异常java.lang.IllegalStateException: Cannot forward after response has been committed -->
<P><input type="submit" value="点击上传" /></P>
<!-- <input type="hidden" name="flag" value="upload" /> -->
</fieldset>
public void upload(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
//实例化JspSmartUpload组件中的SmartUpload对象 该类对象的作用是实现上传
SmartUpload smartUpload = new SmartUpload();
//对 smartUpload对象进行初始化
smartUpload.initialize(this.getServletConfig(), request, response);
try {
//调用upload方法实现文件 该方法会抛出异常
smartUpload.upload();
Request req = smartUpload.getRequest();
String author = req.getParameter("author");
String press = req.getParameter("press");
System.out.println("author="+author);
Files files = smartUpload.getFiles();
for(int i=0;i<files.getCount();i++){
// 返回 JspSmartUpload 组件中的File类,每一个file对象代表一条form表单的文件域
File file = files.getFile(i);
//获取上传文件的文件名
String fileName = file.getFileName();
//将上传的文件保存到指定的位置下
file.saveAs(SAVEPATH+fileName);
//向request作用域中存入信息
request.setAttribute("author", author);
request.setAttribute("press", press);
request.setAttribute("msg", "文件上传成功!");
request.setAttribute("savepath", SAVEPATH);
request.getRequestDispatcher("success.jsp").forward(request, response);
}
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
异常提示:java.lang.IllegalStateException: Cannot forward after response has been committed