servlet3.0新特性测试,文件上传(1)
2014-07-30 12:35
302 查看
servlet代码
页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="a.do" enctype="multipart/form-data">
<input type="file" name="f" >
<input type="submit" value="submit">
</form>
</body>
</html>
问题来了,提交报错严重: Servlet.service() for servlet [test] in context with path [/tomcat7test] threw exception [org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is null] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is null
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:806)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:261)后来查了查资料,form表单必须要设置method=post和enctype=multipart/form-data
本文出自 “helloworld” 博客,请务必保留此出处http://lawrence16.blog.51cto.com/1908331/1532684
@MultipartConfig() @WebServlet(name = "test", urlPatterns = "*.do", initParams = { @WebInitParam(name = "", value = "") }, loadOnStartup = 3) public class TestServlet3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub process(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub process(req, resp); } private void process(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Part p = req.getPart("f"); BufferedReader br = new BufferedReader(new InputStreamReader( p.getInputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); } } }非常简单的代码,读取上传文件内容,然后输出到控制台。
页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="a.do" enctype="multipart/form-data">
<input type="file" name="f" >
<input type="submit" value="submit">
</form>
</body>
</html>
问题来了,提交报错严重: Servlet.service() for servlet [test] in context with path [/tomcat7test] threw exception [org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is null] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is null
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:806)
at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:261)后来查了查资料,form表单必须要设置method=post和enctype=multipart/form-data
本文出自 “helloworld” 博客,请务必保留此出处http://lawrence16.blog.51cto.com/1908331/1532684
相关文章推荐
- servlet3.0新特性测试,文件上传(1)
- 【Servlet 3.0】Servelet 3.0 新特性 文件上传 path对象
- 【Servlet3.0新特性】第03节_文件上传
- [Servlet3.0新特性]Serlvet文件上传
- Servlet 3.0新特性——文件上传接口
- Servlet3.0新特性之文件上传
- Servlet 3.0新特性——文件上传接口
- 【Servlet3.0新特性】第03节_文件上传
- [Servlet3.0新特性]Serlvet文件上传
- Servlet 3.0 新特性详解(五)ServletContext 的性能增强及HttpServletRequest 对文件上传的支持
- Servlet3.0 新特性——HttpServletRequest 对文件上传的支持
- servlet3.0 文件上传功能
- Servlet 3.0笔记之超方便的文件上传支持
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
- [Servlet3.0新特性]注解替代配置文件
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传
- Servlet3.0的文件上传
- web入门-servlet 3.0注解 文件上传
- servlet3.0学习之文件上传
- Servlet3.0学习总结(三)——基于Servlet3.0的文件上传