javac编译时,提示如下:Test.java:40: unreported exception org.apache.commons.fileupload.FileUploadExcep
tion; must be caught or declared to be thrown
List items = sfu.parseRequest(req);
^
1 error
代码如下:package mytest;import java.io.IOException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Iterator;
import java.util.HashMap;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Test extends HttpServlet {
static final long serialVersionUID = 1L; private String fileExt(String fileName)
{
return fileName.substring(fileName.lastIndexOf(".") + 1);
} public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.setCharacterEncoding("UTF-8"); /*
HttpSession sess = reg.getSession();
String uid = (String)sess.getAttribute("uid");
*/ FileItemFactory fif = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(fif);
List items = sfu.parseRequest(req); //here is line 40
Iterator iter = items.iterator();
FileItem item = null;
HashMap<String, String> data = new HashMap<String, String>();
while(iter.hasNext()) {
item = (FileItem)iter.next();
if(item.isFormField())
data.put(item.getFieldName(), item.getString("UTF-8"));
else {
FileOutputStream fos = new FileOutputStream("C:\\Users\\my\\9." + fileExt(item.getName()));
fos.write(item.get());
fos.flush();
fos.close();
}
} data.clear();
}
}我没有try-catch。我的疑问是,为什么会有exception?怎么解决?
谢谢大家!
tion; must be caught or declared to be thrown
List items = sfu.parseRequest(req);
^
1 error
代码如下:package mytest;import java.io.IOException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Iterator;
import java.util.HashMap;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;public class Test extends HttpServlet {
static final long serialVersionUID = 1L; private String fileExt(String fileName)
{
return fileName.substring(fileName.lastIndexOf(".") + 1);
} public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.setCharacterEncoding("UTF-8"); /*
HttpSession sess = reg.getSession();
String uid = (String)sess.getAttribute("uid");
*/ FileItemFactory fif = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(fif);
List items = sfu.parseRequest(req); //here is line 40
Iterator iter = items.iterator();
FileItem item = null;
HashMap<String, String> data = new HashMap<String, String>();
while(iter.hasNext()) {
item = (FileItem)iter.next();
if(item.isFormField())
data.put(item.getFieldName(), item.getString("UTF-8"));
else {
FileOutputStream fos = new FileOutputStream("C:\\Users\\my\\9." + fileExt(item.getName()));
fos.write(item.get());
fos.flush();
fos.close();
}
} data.clear();
}
}我没有try-catch。我的疑问是,为什么会有exception?怎么解决?
谢谢大家!
解决方案 »
- hibernate+sqlserver一对多的插入问题
- 类前修饰符的问题
- 入门级问题对初学者的帮助!!!
- 一个乱码的问题
- JBOSS-3.0.3_TOMCAT4.1.18是啥东东?能下载到吗?
- 我下载并安装一个jsee的jdk,之后再把JCreator的默认jdk设置成为jsee的jdk,就可以用JCreator写和调试servlet的java程序了是么?
- 新的一年来了,散100分,大家都谈谈新的一年的奋斗和学习计划,哈哈
- (求问)各位请问不用struts标签会不会影响到ssh的连接
- 菜鸟问题。
- 针对JDK1.3与J2sdk1.4与Tomcat之间哪个好用的问题,签到有分!
- Timer的问题
- 我觉得未来的网站技术应该是虚拟现实技术来开发的,不知道大家对这个问题是如何看的?
tion; must be caught or declared to be thrown
List items = sfu.parseRequest(req);
^
1 error 另外,我没有对报错的statement用try-catch。
必须捕获异常。你捕获下异常应该就没问题了
try {
items = sfu.parseRequest(req);
} catch (FileUploadException e) {
// TODO 这里根据自己的需求进行处理,可以为空(不建议)
e.printStackTrace();
}
不建议采用 System.out.println(ex.getMessage())
可以使用 e.printStackTrace();
List items = null;
try {
items = sfu.parseRequest(req);
} catch(FileUploaeException ex) {
ex.printStackTrace();
}
//.../* case 2 */
try {
List items = sfu.parseRequest(req);
} catch(FileUploadeException ex) {
ex.printStackTrace();
}
//.../* case 3 */try {
List items = sfu.parseRequest(req);
} catch(FileUploadeException ex) {
ex.printStackTrace();
List items = null;
}
//...
case 1没有问题,case 2和case 3都提示cannot find symbol items。这是什么原因呢?items变量的作用域的问题?