How do I load and save a file in a Java Servlet without using apache?

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>");

      

These codes can help me download the file, but the problem is that after clicking the Download button, I cannot save the downloaded file to a specific directory. Anyone can give any suggestion?

+2


source to share


6 answers


The above code just outputs the HTML to download. It does nothing with upload requests, which may start from a form.



May I ask why you don't want to use Apache Commons FileUpload ? Not using this means that you will need to implement RFC 1867 . A lot of time and effort has been wasted when the implementation already exists.

+2


source


You need to write another servlet (or some CGI, jsp ... etc) to extract the file from the request and save it anywhere:



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

0


source


Apache Commons FileUpload is a way like the others. If you don't want to use this for whatever 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

It's not as reliable as FileUpload, but works great for simple file uploads.

0


source


If you want to use Multipart request, you need to write your servoupload servlet to handle this, for example:

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 be outputting pure html from a servlet like in your question - try submitting to jsp - even better if nothing else is required, just use plain html.

0


source


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

0


source


I second suggestion of mlk and think reading the Commons FileUpload User Guide will get you started. It will handle getting the file, but you still have to tell it "where" to save it. From your description it sounds like you want the user to select "where" to store the file. You will have to write this part yourself.

I hacked a fast lister in a servlet. All other comments are correct. It's not a good idea to write html in a servlet, but it 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>");
    }
}

      

0


source







All Articles