JSP Interface Code:
<body>
<form action= "Servlet/uploadservlet" enctype= " Multipart/form-data " method=" post "
<table>
<tr>
<td> file upload: </td>
<td>
<input type= "file" name= "File1" >
</td>
</tr>
<tr>
<td>
<input type= "Submit" value= "Submission";
</td
</tr>
</table>
</form>
</body>
Background code:
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Response.setcontenttype ("text/html");
File file1 = null;
Diskfileupload diskfileupload = new Diskfileupload ();
try {
list<fileitem> list = diskfileupload.parserequest (request);
for (Fileitem fileitem:list) {
if (Fileitem.isformfield ()) {
}
else{
File RemoteFile = new file (new String (Fileitem.getname (). GetBytes (), "UTF-8")); Server Project Deployment Path _e:\tomcat\tomcat\webapps\fileuploaddemo\attachment
File1 = new File (This.getservletcontext (). Getrealpath ("attachment"), Remotefile.getname ());
File1.getparentfile (). Mkdirs ();
File1.createnewfile ();
InputStream INP = Fileitem.getinputstream ();
OutputStream OUP = new FileOutputStream (file1);
byte[] buffer = new byte[1024];
int len = 0;
while (len = inp.read (buffer)) >-1) {
Oup.write (buffer); }
Oup.close ();
Inp.close ();
}
}
} catch (Fileuploadexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Java File Upload