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?
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.
source to share
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.
source to share
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>");
}
}
source to share