Many java internet developer conscious that for importing information on server there was no api assist in servlet specs. We’ve to depend upon third occasion library to add information. Apache common’s file upload library may be very well-liked for this process.
Servlet Three comes with default assist of file add. To make your servlet multipart conscious builders suppose to make use of @MultipartConfig
annotation. @MultipartConfig
annotation is used to point that the Servlet on which it’s declared expects requests to made utilizing the multipart/form-data MIME sort.
To make use of servlet 3.Zero api’s you must deploy your software on Java EE 6 conscious internet/app server. I’m utilizing Apache Tomcat 7.x which comes with servlet Three assist.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package deal org.techzoo.servlet3;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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.Half;
@WebServlet (“/add”)
@MultipartConfig
public class FileUploadServlet extends HttpServlet {
non-public static ultimate lengthy serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
uploadFile(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
uploadFile(request, response);
}
/**
* uploadFile technique used to add file to server.
*
***/
non-public void uploadFile(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
for(Half half : request.getParts()) {
String title = half.getName();
InputStream is = request.getPart(title).getInputStream();
String fileName = getUploadedFileName(half);
FileOutputStream fos = new FileOutputStream(“c:/add/”+fileName);
int knowledge = 0;
whereas((knowledge = is.learn()) != –1) {
fos.write(knowledge);
}
fos.shut();
is.shut();
response.sendRedirect(“success.jsp”);
}
}
/**
* Technique used to get uploaded file title.
* It will parse following string and get filename
* Content material-Disposition: form-data; title=”content material”; filename=”a.txt”
**/
non-public String getUploadedFileName(Half p) {
String file = “”, header = “Content material-Disposition”;
String[] strArray = p.getHeader(header).cut up(“;”);
for(String cut up : strArray) {
if(cut up.trim().startsWith(“filename”)) {
file = cut up.substring(cut up.indexOf(‘=’) + 1);
file = file.trim().exchange(“””, “”);
System.out.println(“File title : “+file);
}
}
return file;
}
}
|
Now create a jsp web page with a kind tag. This manner tag should have attribute enctype="multipart/form-data"
.
1
2
3
4
|
<kind motion=“add” enctype=“multipart/form-data” technique=“publish”>
<enter sort=“file” title=“uploadFile” />
<enter sort=“submit” worth=“Add File” />
</kind>
|
Output will seem like as following screenshot.
Supply techzoo.org