To know extra about the right way to use servlet Three for file add, please go to my servlet 3 file upload publish.
Obtain and add apache file add, frequent io jar information into eclipse library. Following screenshot reveals eclipse folder construction.
First create a HTML kind to add tune mp3 file together with its description. To add any content material on server your kind should have enctype="multipart/form-data"
attribute.
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
|
<kind motion=“add” methodology=“publish” enctype=“multipart/form-data”>
<desk width=“358” border=“0” cellspacing=“0” cellpadding=“0” class=“desk”>
<tr>
<td width=“144”><sturdy>Movie Title</sturdy></td>
<td width=“8”> </td>
<td width=“203”><label>
<enter kind=“textual content” title=“movie” />
</label></td>
</tr>
<tr>
<td><sturdy>Lyric</sturdy></td>
<td> </td>
<td><enter kind=“textual content” title=“lyric” dimension=“40”/></td>
</tr>
<tr>
<td><sturdy>Music</sturdy></td>
<td> </td>
<td>
<choose title=“music”>
<possibility worth=“A. R. Rehman”>A. R. Rehman</possibility>
<possibility worth=“Ram Sampat”>Ram Sampat</possibility>
</choose>
</td>
</tr>
<tr>
<td><sturdy>Add</sturdy></td>
<td> </td>
<td>
<enter kind=“file” title=“tune” /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<enter kind=“submit” title=“Submit” worth=“Add Music” class=“button”/>
</td>
</tr>
</desk>
</kind>
|
Following servlet class used to add mp3 file on server listing.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
bundle org.techzoo.add;
import java.io.File;
import java.io.IOException;
import java.util.Checklist;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet(“/add”)
public class SongUploadServlet extends HttpServlet {
non-public static remaining lengthy serialVersionUID = 1L;
non-public static remaining int THRESHOLD_SIZE = 1024 * 1024 * 3; // 3MB
non-public static remaining int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
non-public static remaining int REQUEST_SIZE = 1024 * 1024 * 50; // 50MB
non-public Checklist<FileItem> fileItem = null;
protected Checklist<FileItem> initRequest(HttpServletRequest req) {
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if(!isMultipart) throw new UnsupportedOperationException();
DiskFileItemFactory manufacturing unit = new DiskFileItemFactory();
manufacturing unit.setSizeThreshold(THRESHOLD_SIZE);
manufacturing unit.setRepository(new File(System.getProperty(“java.io.tmpdir”)));
ServletFileUpload add = new ServletFileUpload(manufacturing unit);
add.setFileSizeMax(MAX_FILE_SIZE);
add.setSizeMax(REQUEST_SIZE);
Checklist<FileItem> formItems = null;
attempt {
formItems = add.parseRequest(req);
} catch (Exception e) {
e.printStackTrace();
}
return formItems;
}
protected String getFieldValue(Checklist<FileItem> formItems, String fieldName)
{
String worth = null;
attempt {
for(FileItem fi : formItems )
{
if (fi.isFormField())
{
if(fi.getFieldName().equals(fieldName))
{
worth = fi.getString();
}
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return worth;
}
protected File uploadFile(Checklist<FileItem> formItems, String destFolder)
{
String uploadPath = destFolder;
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
File uploadedFile = null;
attempt {
for(FileItem fi : formItems ){
if (!fi.isFormField()) {
String fileName = new File(fi.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
uploadedFile = new File(filePath);
fi.write(uploadedFile);
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return uploadedFile;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
fileItem = initRequest(request);
String movie = getFieldValue(fileItem, “movie”);
String music = getFieldValue(fileItem, “music”);
String lyric = getFieldValue(fileItem, “lyric”);
File file = uploadFile(fileItem, “c:/add”);
Music tune = new Music(movie, lyric, music, file.getName());
request.setAttribute(“tune”, tune);
RequestDispatcher rd = request.getRequestDispatcher(“/success.jsp”);
rd.ahead(request, response);
}
}
|
When consumer fills the shape and click on on add tune button, mp3 tune will add on server and servlet will create Music
object, set in request attribute and dispatch the request to success.jsp
web page.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
bundle org.techzoo.add;
public class Music {
non-public String movie,lyric, music,fileName;
public Music(){}
public Music(String movie, String lyric, String music, String fileName) {
tremendous();
this.movie = movie;
this.lyric = lyric;
this.music = music;
this.fileName = fileName;
}
// setter, getter eliminated for abbreviation.
}
|
and Output will look much like following picture…
Supply techzoo.org