| Server IP : 23.94.149.180 / Your IP : 216.73.216.147 Web Server : nginx/1.20.1 System : Linux deadrock 5.14.0-741.22.1.el9_9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Nov 12 15:18:14 UTC 2026 x86_64 User : apache ( 48) PHP Version : 8.6.0 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /files/www/voxsupport/files/fancy/docs/userguide/HTML/ |
Upload File : |
<html>
<head>
<title>Servlet example</title>
<meta name="generator" content="Help & Manual">
<meta name="keywords" content="">
</head>
<!-- Redirect browser to index page if page is not in the content frame.
This script is only valid for regular HTML export -->
<script language="JavaScript">
<!--
if(top.frames.length==0) top.location.href='index.html?servletexample.html'
//-->
</script>
<body bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="2" bgcolor="#E2E2E2">
<tr>
<td align="left">
<span style="font-family:Helvetica,Arial; font-size:12pt; color:#000000"><span style="font-family:Helvetica,Arial; font-size:12pt; color:#000080"><b>Servlet example
<br>
</b></span></span>
</td>
<td align="right">
<font face="Arial" size="2">
<a href="javaScript:parent.reDisplay('1.1',1,0)"><img src="ico_nav_up.gif" border=0 alt="Top"></a>
<a href="javaScript:parent.reDisplay('3.4',1,0)"><img src="ico_nav_back.gif" border=0 alt="Previous"></a>
</font>
</td>
</tr>
</table>
<br>
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">The example below demonstrates how using a Java servlet you can process files submitted by HTTP File Upload Applet. The source code for this example may be found in the <span style="font-family:Courier New; font-size:10pt; color:#000000">examples/servlet/Receiver.java</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> file included with the HTTP File Upload Applet distribution. The file upload classes found in the Jakarta FileUpload package are required for this example to work. You may download the Jakarta FileUpload classes here:
<br>
<br>
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#0000FF"><u><span class="weblink"><a href="http://jakarta.apache.org/commons/fileupload/" target="_blank">http://jakarta.apache.org/commons/fileupload/</a></span></u></span></span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
<br>
<br>
<span style="font-family:Helvetica,Arial; font-size:10pt; color:#008000"><b>Example</b></span></span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
<br>
<br>
</span><span style="font-family:Courier New; font-size:10pt; color:#000000">import java.io.File;
<br>
import java.io.IOException;
<br>
import java.io.PrintWriter;
<br>
import java.util.*;
<br>
<br>
import javax.servlet.ServletException;
<br>
import javax.servlet.http.HttpServlet;
<br>
import javax.servlet.http.HttpServletRequest;
<br>
import javax.servlet.http.HttpServletResponse;
<br>
<br>
import org.apache.commons.fileupload.*;
<br>
import org.apache.commons.fileupload.disk.*;
<br>
import org.apache.commons.fileupload.servlet.*;
<br>
<br>
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">public</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> <span style="font-family:Courier New; font-size:10pt; color:#0000FF">class</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> Receiver extends HttpServlet {
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">private</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> String uploadDir = <span style="font-family:Courier New; font-size:10pt; color:#000080">"/var/uploads/"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">;
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">private</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> String file = <span style="font-family:Courier New; font-size:10pt; color:#000080">""</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">;
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">private</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> String currentDirectory = <span style="font-family:Courier New; font-size:10pt; color:#000080">""</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">;
<br>
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">protected</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> <span style="font-family:Courier New; font-size:10pt; color:#0000FF">void</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// add connection closed header</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
response.setHeader(<span style="font-family:Courier New; font-size:10pt; color:#000080">"Connection"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">,<span style="font-family:Courier New; font-size:10pt; color:#000080">"close"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// Create PrintWriter object to write to</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
PrintWriter out = response.getWriter();
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">try</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> {
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// Create a factory for disk-based file items</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
FileItemFactory factory = <span style="font-family:Courier New; font-size:10pt; color:#0000FF">new</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> DiskFileItemFactory();
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// Create a new file upload handler</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
ServletFileUpload upload = <span style="font-family:Courier New; font-size:10pt; color:#0000FF">new</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> ServletFileUpload(factory);
<br>
<br>
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// Parse the request, we got the list of items present in POST header</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
List items = upload.parseRequest(request);
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">for</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> (<span style="font-family:Courier New; font-size:10pt; color:#0000FF">int</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> i = <span style="font-family:Courier New; font-size:10pt; color:#000080">0</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">; i < items.size(); i++) {
<br>
DiskFileItem item = (DiskFileItem) items.get(i);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// As we are interested not in regular form fields, we filter only files</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">if</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> (!item.isFormField()) {
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// the name of file to save</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
String fileName = item.getName();
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>//Split file name and directory name when this is specified</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
(fileName);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>//creates the directory</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
createDirectory(this.currentDirectory);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// concat the default directory, the specified directory and the file name </i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
String completeName = (this.currentDirectory.equals(<span style="font-family:Courier New; font-size:10pt; color:#000080">""</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">)?
<br>
uploadDir + this.file:
<br>
uploadDir + this.currentDirectory +<span style="font-family:Courier New; font-size:10pt; color:#000080">"\\"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">+ this.file);
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// save file</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
File uploadedFile = <span style="font-family:Courier New; font-size:10pt; color:#0000FF">new</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> File(completeName);
<br>
item.write(uploadedFile);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>//if succeeded, print out the "success" code to notify applet the file was uploaded </i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
out.print(<span style="font-family:Courier New; font-size:10pt; color:#000080">"RESP.100"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>//flush the stream to speed up applet notification</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
out.flush();
<br>
}
<br>
}
<br>
} <span style="font-family:Courier New; font-size:10pt; color:#0000FF">catch</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> (FileUploadException e) {
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>//if failed for some reason, print out the "failed" code to notify applet the file wasn't uploaded </i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
out.print(<span style="font-family:Courier New; font-size:10pt; color:#000080">"RESP.200"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>// flush the stream to speed up applet notification</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
out.flush();
<br>
e.printStackTrace();
<br>
} <span style="font-family:Courier New; font-size:10pt; color:#0000FF">catch</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> (Exception e) {
<br>
e.printStackTrace();
<br>
}
<br>
}
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>/**
<br>
* Separates the file name and the directory name if the directory is specified
<br>
*
<br>
* @param fileName The file name which is sent from the client
<br>
*
<br>
*/</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">private</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> <span style="font-family:Courier New; font-size:10pt; color:#0000FF">void</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> splitFileFromDirectory(String fileName){
<br>
this.currentDirectory = <span style="font-family:Courier New; font-size:10pt; color:#000080">""</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">;
<br>
this.file =<span style="font-family:Courier New; font-size:10pt; color:#000080">""</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">;
<br>
String[] data = fileName.split(<span style="font-family:Courier New; font-size:10pt; color:#000080">"\\\\"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">if</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">(data.length!=<span style="font-family:Courier New; font-size:10pt; color:#000080">1</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">){
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">int</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> index = fileName.lastIndexOf(<span style="font-family:Courier New; font-size:10pt; color:#000080">"\\"</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">);
<br>
this.file = fileName.substring(index+<span style="font-family:Courier New; font-size:10pt; color:#000080">1</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">, fileName.length());
<br>
this.currentDirectory = fileName.substring(<span style="font-family:Courier New; font-size:10pt; color:#000080">0</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">, index);
<br>
}
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">else</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">{
<br>
this.file = fileName;
<br>
}
<br>
<br>
}
<br>
<br>
<span style="font-family:Courier New; font-size:10pt; color:#008000"><i>/**
<br>
* Creates a new directory name if it does not exist
<br>
*
<br>
* @param directory The name of the directory
<br>
*/</i></span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">private</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> <span style="font-family:Courier New; font-size:10pt; color:#0000FF">void</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> createDirectory(String directory){
<br>
String newDirectory = uploadDir + directory;
<br>
File directoryFile = <span style="font-family:Courier New; font-size:10pt; color:#0000FF">new</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000"> File(newDirectory);
<br>
<span style="font-family:Courier New; font-size:10pt; color:#0000FF">if</span></span><span style="font-family:Courier New; font-size:10pt; color:#000000">(!directoryFile.isDirectory()){
<br>
directoryFile.mkdirs();
<br>
}
<br>
}
<br>
}</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
<br>
<b>
<br>
Installation</b>
<br>
<br>
</span></span><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="28"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">1.</span></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Change <span style="font-family:Courier New; font-size:10pt; color:#000000">uploadDir</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> variable to directory path where files should be uploaded. Make sure to include a </span><span style="font-family:Courier New; font-size:10pt; color:#000000">/</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> at the end and that the permissions to write are set properly.
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="28"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">2.</span></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Place required <span style="font-family:Courier New; font-size:10pt; color:#000000">commons-fileupload-1.0.jar</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> in </span><span style="font-family:Courier New; font-size:10pt; color:#000000">WEB-INF/lib</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> directory of application server. This jar file is part of the Jakarta FileUpload package.
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="28"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">3.</span></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Compile <span style="font-family:Courier New; font-size:10pt; color:#000000">Receiver.java</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> code and upload compiled </span><span style="font-family:Courier New; font-size:10pt; color:#000000">Receiver.class</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> file to </span><span style="font-family:Courier New; font-size:10pt; color:#000000">WEB-INF/classes</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"> directory on application server.
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="28"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">4.</span></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Configure servlet in web.xml descriptor <span style="font-family:Courier New; font-size:10pt; color:#000000">/WEB-INF/web.xml</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><span style="font-family:Courier New; font-size:10pt; color:#000000">...
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> <servlet>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> <servlet-name>receiver</servlet-name>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> <servlet-class>Receiver</servlet-class>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> </servlet>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> <servlet-mapping>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> <url-pattern>/receiver</url-pattern>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> <servlet-name>receiver</servlet-name>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"> </servlet-mapping>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000">...
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Courier New; font-size:10pt; color:#000000"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="28"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">5.</span></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Set url parameter in HTML applet code to point to url of this script e.g. <span style="font-family:Courier New; font-size:10pt; color:#000000">http://www.jscape.com/appname/receiver</span><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span></span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="28"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">6.</span></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">Done. You may now run HTTP File Upload Applet
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><b>See also</b>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"></span></td><td></td></tr></table><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"></span><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="14"></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><a href=javaScript:parent.reDisplay('2.8',1,0)>Specifying the upload URL</a>
<br>
</span></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td width="14"></td><td><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"></span></td></tr></table><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000"><span style="font-family:Helvetica,Arial; font-size:10pt; color:#000000">
<br>
</span><span style="font-family:Courier New; font-size:10pt; color:#000000">
<br>
</span></span>
</body>
</html>