3
out.println("<tr><td><FORM ENCTYPE='multipart/form-data'"+
                          "method='POST' action='ProcessUpload' ></td>"+
                          "<td><INPUT TYPE='file' NAME='mptest'></td>"+
                          "<td><INPUT TYPE='submit' VALUE='upload'></td>"+
                          "</FORM></tr>");

This codes can help me upload file but the problem is after I click upload, I cant save the uploaded file in particular directory.Anyone can give some suggestion?

2
  • 1
    What do you mean with "apache"? Do you mean the commons library that handles multipart requests, do you mean Tomcat in general? Commented Sep 23, 2009 at 9:01
  • ya i mean commons library that handle multipart requests. Sry,i did't use Tomcat. Commented Sep 23, 2009 at 11:49

6 Answers 6

2

The code above simply outputs the HTML for an upload button. It does not do anything with any upload requests that form might start.

May I ask why you don't want to use Apache Commons FileUpload? To not use it will mean that you will need to implement RFC 1867. A lot of time and effort wasted when an implementation already exists.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually i don't how to implement the Apache FileUpload into my servlet. so plan to find one simple way to upload and save file. i try to import the apache but seem like need download the apache api. i still new to java so cant sure how to do it. Can you please give me some idea to do it? thx a lot .
Using 3rd party libraries is a core part of working with Java. I feel this is something you should know before working with servlets but each to their own. How you include a 3rd party library depends on your build system & IDE. Yes to start with you need to download the library, you can find the download link in the link to Commons FileUpload above. Then you need to put it into your classpath. In some cases this can be as simple as throwing it in the WEB-INF\lib of your project. In others (such as Maven or Ivy) you will need to edit project files and the build tool will handle the details.
I should also add that Commons FileUpload depends on Commons IO. The download link for this can also be found in the link for FileUpload above. This will also need to be included in your project to use FileUpload.
0

You have to write another servlet (or some CGI, jsp ...etc.) to retrieve the file from the request and save it to wherever you like:

http://www.frontiernet.net/~Imaging/FileUploadServlet.html

Comments

0

Apache Commons FileUpload is the way to go as others suggested. If you don't want use that for any reason, you can also look at this class,

http://metatemplate.googlecode.com/svn/trunk/metatemplate/java-app-framework/tomcat-adapter/src/com/oreilly/servlet/MultipartRequest.java

This is not as robust as FileUpload but it works fine for simple file upload.

1 Comment

i did't use tomcat , i use Glassfish. Thx
0

If you want to use Multipart request you need to write your processUpload servlet to handle this eg:

private String fileSavePath;

public void init(){
    fileSavePath = getServletContext().getRealPath("/") + "data";
}   


public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws IOException, ServletException{
    MultipartRequest mpr = new MultipartRequest(request, fileSavePath);
}

And I really wouldn't output pure html from a servlet as in your question - try dispatching to a jsp - even better if nothing else is required just use plain html.

Comments

0

The COS libary http://servlets.com/cos/ (not apache)

Comments

0

I second mlk's suggestion and think reading the Users Guide to Commons FileUpload will help you get started. It will handle receiving the file, but you still have to tell it "where" to store it. From your description, sounds like you want the user to choose "where" to store the file. You will have to write this portion yourself.

I hacked together a quick lister in a servlet. All the other comments are correct. Not a good idea to write html in a servlet, but this sounds like a good learning experience.

package somepackage;

import java.io.File;
import java.io.IOException;

import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DirectoryChooserServlet extends HttpServlet {
    public DirectoryChooserServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {

        Writer w = response.getWriter();
        w.write("<html><body>");
        String action = request.getParameter("action");
        String directory = request.getParameter("directory");
        String startDirectory = "/private";
        if ("list".equals(action)) {
            startDirectory = directory;
        }
        File dir = new File(startDirectory);
        if (dir != null) {
            w.write("<a href=\"?action=list&directory="+dir.getParentFile().getAbsolutePath()+"\">..</a><br/>");
            for(File f: dir.listFiles()) {
                if(f.isDirectory()) {
                    w.write("<a href=\"?action=list&directory="+f.getAbsolutePath()+"\">" + f.getName() + "</a><br/>");    
                }            
            }
        }
        w.write("</body></html>");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.