使用Apache Tomcat 7.0.40.0时servlet中的文件上载错误 [英] File upload error in servlet while using Apache Tomcat 7.0.40.0

查看:206
本文介绍了使用Apache Tomcat 7.0.40.0时servlet中的文件上载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Apache Tomcat 7.0.34使用org.apache.tomcat.util.fileupload进行文件上传时,不会显示任何错误,一切正常。但是当我使用Apache Tomcat 7.0.40时,parseRequest(request)行中出现了一个错误。我无法将此视为错误,因为如果我使用RequestContext,那么错误将会消失,但我不知道如何使用RequestContext接口。请帮我如何使用RequestContext,因为我需要将实例传递给parseRequest(RequestContext ctx)方法。

When I use Apache Tomcat 7.0.34 for file uploading using "org.apache.tomcat.util.fileupload" no error is displayed and everything works fine. But when I use Apache Tomcat 7.0.40 one error occurred in the line "parseRequest(request)". I can't tell this as an error because if I use RequestContext then the error will go but I don't know how to use RequestContext Interface. Please help me how to use RequestContext because I need to pass the instance to "parseRequest(RequestContext ctx)" method.

 public void service(HttpServletRequest request,HttpServletResponse response)
{
    response.setContentType("text/html;charset=UTF-8");         

    String status=null;
    List<FileItem> items=null;

    try
    {      
        if(ServletFileUpload.isMultipartContent(request))
        {             
            items=new ServletFileUpload(new    
    DiskFileItemFactory()).parseRequest(request);
            for(FileItem item:items)
            {                   
                if(item.getFieldName().equals("status"))
                    status=item.getString();
            }  
        }
   }
    catch(Exception e)
    {
       e.printStackTrace();
    }
}   

我需要将RequestContext实例放在parseRequest中(RequestContext ctx) )但不知道如何使用RequestContext。

I need to put RequestContext instance inside parseRequest(RequestContext ctx) but don't know how to use RequestContext.

推荐答案

这是正确的方法处理Servlet 3.0中的文件上传。您应该使用 @ servlet上的MultipartConfig 注释并使用 HttpServletRequest#getPart() 获取上传文件的方法,该文件是在Servlet 3.0中引入的。

This is not the right way to process a file upload in Servlet 3.0. You should instead be using @MultipartConfig annotation on the servlet and be using HttpServletRequest#getPart() method to obtain the uploaded file, which was introduced in Servlet 3.0.

org.apache.tomcat.util.fileupload 包正好包含那些正在幕后的所有类这个新的Servlet 3.0功能的工作。您不应该直接使用它们,就像您在Sun / Oracle JVM上使用Java SE时不应该使用 sun。* 类一样,并且您不应该这样做在MySQL数据库上使用JDBC时,不要使用 com.mysql。* 类。您似乎对使用 Apache Commons FileUpload 的Servlet 2.5或更早版本的示例感到困惑相同的类名。

The org.apache.tomcat.util.fileupload package contains exactly those classes which are doing all "behind the scenes" work of this new Servlet 3.0 feature. You shouldn't be using them directly, like as that you shouldn't be using sun.* classes when using Java SE on a Sun/Oracle JVM, and that you shouldn't be using com.mysql.* classes when using JDBC on a MySQL DB. It seems that you got confused by examples targeted at Servlet 2.5 or older using Apache Commons FileUpload which happens to use the same classnames.

使用特定于Tomcat的类会将您的webapp紧密耦合到特定的Tomcat版本,并使您的webapp 不可移植到其他Servlet 3.0兼容的容器,甚至你自己遇到的不同的Tomcat版本。在这种特殊情况下,你应该坚持使用 javax.servlet 包中的标准类。

Using Tomcat-specific classes would tight-couple your webapp to the specific Tomcat version and makes your webapp unportable to other Servlet 3.0 compatible containers and even to a different Tomcat version as you encountered yourself. You should in this particular case stick to standard classes from the javax.servlet package.

显示正确的方法在这个答案的第二部分:如何使用JSP / Servlet将文件上传到服务器?

The right way is shown in the 2nd part of this answer: How to upload files to server using JSP/Servlet?

总而言之,这个启动示例可以帮助您入门:

All with all, this kickoff example should get you started:

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="text" name="status" />
    <input type="file" name="uploadedFile" />
    <input type="submit" />
</form>

with

@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String status = request.getParameter("status"); // Retrieves <input type="text" name="status">
        Part uploadedFile = request.getPart("uploadedFile"); // Retrieves <input type="file" name="uploadedFile">
        InputStream content = uploadedFile.getInputStream();
        // ... (do your job here)
    }

}

这就是全部。

这篇关于使用Apache Tomcat 7.0.40.0时servlet中的文件上载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆