Here is the code for my servlet, and form:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bluelotussoftware.example.servlet; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.MultipartConfig; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.Part; | |
/** | |
* | |
* @author John Yeary | |
* @version 1.0 | |
*/ | |
@WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"}) | |
@MultipartConfig(location = "/tmp") | |
public class FileUploadServlet extends HttpServlet { | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
for (Part part : request.getParts()) { | |
String filename = ""; | |
for (String s : part.getHeader("content-disposition").split(";")) { | |
if (s.trim().startsWith("filename")) { | |
filename = s.split("=")[1].replaceAll("\"", ""); | |
} | |
} | |
part.write(filename); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>File Upload Example</title> | |
</head> | |
<body> | |
<form action="FileUploadServlet" enctype="multipart/form-data" method="POST"> | |
<input type="file" name="file"> | |
<input type="Submit" value="Upload File"> | |
</form> | |
</body> | |
</html> |
8 comments :
Nice example. According path used in example, you are on unix based machine(Mac?) or linux. i am using win7 and i have sligthly changed code
@MultipartConfig(location="C:/temp")
works fine, except it breaks cyrylic filenames but it is a encoding issue.
thanks
You are correct sir! I am using a Mac. Thanks for the additional information for Windows users.
i wonder in case when the big files are uploaded, will the parts be available when all the content is parsed & saved/cache somewhere by the jvm or or they streamed ?
thanx
I have not tried it on any large files. There are usually practical limits to uploading files using an HTML uploader. If you have some large files to upload, I would be interested in seeing what difficulties if any that you encounter.
The example was not meant to be a complete solution. It was a simple demonstration of what is possible.
sorry not to be more specific in my question.
if there're 2 files in the form to be uploaded, will this line:
for (Part part : request.getParts()) ...
get hit right after the first part comes to server of after both files have already landed/transfered to the webserver ?
thanx.
@Kim From John's code, all the files are sent to the server at the same time and processed in the loop. So every file found in the multipart object is written to the server.
Also you can write individual files by doing
Part multipartfile = request.getPart("filename");
multipartfile.write(new File("/interesting/location/file"))
The code for uploading depends on the browser for uploads. Some allow multiple uploads, and some don't.
For some examples of file uploaders see my other posts.
http://javaevangelist.blogspot.com/2011/12/multiple-file-upload-examples.html
http://javaevangelist.blogspot.com/2010/12/multiple-file-upload-options.html
Thanks for adding information @Unekwu which is more clarifying.
Post a Comment