tags:

views:

723

answers:

6
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?

A: 

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

Winston Chen
+2  A: 

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.

mlk
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 .
kento
mlk
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.
mlk
i done d thx a lot ~~~~
kento
A: 

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.

ZZ Coder
i did't use tomcat , i use Glassfish. Thx
kento
A: 

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.

Robert
A: 

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

Pierre
A: 

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>");
    }
}
Wayne Young