1

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();
    }
}   

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

matt b
  • 138,234
  • 66
  • 282
  • 345
Ajeesh
  • 1,572
  • 3
  • 19
  • 32
  • 3
    "One error occurred in the following line" - what error, and on what line? We can't see your screen and we can't guess what the error might have been because there are many possible things that can go wrong. Please edit your post to include details of the error (e.g. if it's an exception, include the exception and the stacktrace). – Luke Woodward May 27 '13 at 08:03
  • are you getting error at this line `DiskFileItemFactory()).parseRequest(request);` ? – Rohan May 27 '13 at 09:14
  • Do i Have to set the file permission at the Tomcat Server ? – Jeff Bootsholz Jul 12 '13 at 04:33
  • Not necessary because tomcat always dynamically creates the following file permissions:permission java.io.FilePermission "** your application context**", "read"; permission java.io.FilePermission "** application working directory**", "read,write"; permission java.io.FilePermission "** application working directory**/-", "read,write,delete"; Referred from http://tomcat.apache.org/tomcat-7.0-doc/security-manager-howto.html – Ajeesh Jul 12 '13 at 10:05
  • As Luke Woodward said, a lot more info is needed to help you with this – matt b Jul 12 '13 at 12:31

1 Answers1

4

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.

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.

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.

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)
    }

}

That's all.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am going to implement the file upload from Android tablet to the servlet in the remote tomcat server by packing the httppost as the sending request instead of the fileStream. Is this method also feasible as long as the Servlet file is here with all required jars ? – Jeff Bootsholz Jul 13 '13 at 07:56
  • This doesn't require any additional JARs. This is already out the box supported in Servlet 3.0. Perhaps there is where you made the mistake: you should **absolutely not** have copies of servlet-container specific JARs in your webapp. See also e.g. http://stackoverflow.com/q/4076601/ As to the HttpPost in Android, see also http://stackoverflow.com/a/6917303 and http://stackoverflow.com/a/5205548. Note: also this doesn't require any additional JARs, it's already out the box supported in Android. – BalusC Jul 13 '13 at 13:10