After wasting 2 days I came to know how to upload multiple
files on server, I searched many times on
internet but did not get
solution ,now i can take breath :P its very important code.
i know the value of this code :)
this is for updated servlet3
keep in mind
1.Do not forget to use
enctype="multipart/form-data" in form action of jsp
2.Do not forget to use
@MultipartConfig(maxFileSize = 1024 * 1024 *
50, maxRequestSize = 1024 * 1024 * 100)
if
you dont use it then you will get Null pointer exception and believe me even
after debugging you will not come to know exact reason, :)
Jsp page
<input type="file" name="files1" >
<input type="file" name="files2" >
<input type="file" name="files3" >
Here name of file is different
Now servlet code
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
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;
@MultipartConfig(maxFileSize = 1024 *
1024 * 50, maxRequestSize = 1024 * 1024 * 100)
// 5MB,and 100MB
@WebServlet("/Add_admin_user")
public class Add_admin_user extends HttpServlet {
private static final long serialVersionUID = 1L;
response.setContentType("text/html;charset=UTF-8");
PrintWriter
out = response.getWriter();
// for multiple files upload
String
filesFolder = "/filesFolder/";
here getting exact path of project
String
appPath_for_multileFiles = request.getServletContext()
.getRealPath(filesFolder);
File
multifile = new File(appPath_for_multileFiles);
if
(!multifile.exists()) {
multifile.mkdir();
}
See here if you forget to create new folder then it will create folder on server
//for file 1
Part
part1 = request.getPart("files1");
if (part1 != null) {
if (part1.getSize() !=
0) {
String
fileName1 = extractgetFileName(part1);
if (fileName1 != null) {
part1.write(appPath_for_multileFiles
+ File.separator
+
fileName1);
String filePath1 = appPath_for_multileFiles + File.separator
+
fileName1;
System.out.println("customer
filePath1 full path " + filePath1);
}
}
}
//for file 2
Part part2 = request.getPart("files2");
if (part2 != null) {
if (part2.getSize() !=
0) {
String
fileName2 = extractFileName(part2);
if (fileName2 != null) {
part2.write(appPath_for_multileFiles
+ File.separator
+
fileName2);
String
filePath2 = appPath_for_multileFiles + File.separator
+
fileName2;
System.out.println("customer
filePath2 full path " + filePath2);
}
}
}
//for file 3
Part
part3 = request.getPart("files3");
if (part3 != null) {
if (part3.getSize() !=
0) {
String fileName3 =
extractFileName(part3);
if (fileName3 != null) {
part3.write(appPath_for_multileFiles
+ File.separator
+
fileName3);
String
filePath3 = appPath_for_multileFiles + File.separator
+
fileName3;
System.out.println("customer
filePath3 full path " + filePath3);
}
}
}
private String extractgetFileName(Part
part) {
String
contentDisp = part.getHeader("content-disposition");
String[]
items = contentDisp.split(";");
for (String s : items) {
if
(s.trim().startsWith("filename")) {
return
s.substring(s.indexOf("=") + 2, s.length() - 1);
}
}
return "";
}
if any query then put your questions in comment box.
if any query then put your questions in comment box.
Thanks
Happy Keep Learning
No comments:
Post a Comment