How do I set to location of where to store file uploads on a TOMCAT server?
I am using commons.fileupload
and as it stands I am able to store multiple .tmp
files to catalina_base/temp
however, my aim is to store the uploaded folders in their original form to d:\\dev\\uploadservlet\\web\\uploads
I know this question is vague but to be honest I've been working with servlets for such a short amount of time and I don't yet understand the big picture, any suggestions of code or links to tutorials would be greatly appreciated.
My servlet code which handles the uploads is as follows:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.oreilly.servlet.MultipartRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final long MAX_UPLOAD_IN_MEGS = 5;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//This is the folder I want to use!!
//String uploadFolder = "d:\\dev\\uploadservlet\\web\\uploads";
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
if (!isMultipartContent) {
out.println("Upload unsuccessful<br/>");
return;
}
out.println("The following was uploaded:<br/>");
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(MAX_UPLOAD_IN_MEGS * 1024 * 1024);
TestProgressListener testProgressListener = new TestProgressListener();
upload.setProgressListener(testProgressListener);
HttpSession session = request.getSession();
session.setAttribute("testProgressListener", testProgressListener);
try {
List<FileItem> fields = upload.parseRequest(request);
out.println("Number of fields: " + fields.size() + "<br/><br/>");
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext()) {
out.println("No fields found");
return;
}
out.println("<table border=\"1\">");
while (it.hasNext()) {
out.println("<tr>");
FileItem fileItem = it.next();
boolean isFormField = fileItem.isFormField();
if (isFormField) {
out.println("<td>regular form field</td><td>FIELD NAME: " + fileItem.getFieldName() +
"<br/>STRING: " + fileItem.getString()
);
out.println("</td>");
} else {
out.println("<td>file form field</td><td>FIELDNAME: " + fileItem.getFieldName() +// <br/>STRING: " + fileItem.getString() +
"<br/>NAME: " + fileItem.getName() +
"<br/>CONTENT TYPE: " + fileItem.getContentType() +
"<br/>SIZE (BYTES): " + fileItem.getSize() +
"<br/>TO STRING: " + fileItem.toString()
);
out.println("</td>");
}
out.println("</tr>");
}
out.println("</table>");
} catch (FileUploadException e) {
out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
...and it get's it's information from this HTML form:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload Page</title>
<link rel="stylesheet" type="text/css" href="css/ui-lightness/jquery-ui-1.8.24.custom.css">
<link rel="stylesheet" type="text/css" href="css/style.css"
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<script type="text/javascript" src="uploadFunctions.js"></script>
</head>
<body>
<div>
<form name="uploadForm" id="uploadForm" action="test" method="post" enctype="multipart/form-data">
<input type="hidden" name="hiddenfield1" value="ok">
<h3>Files to upload:</h3>
<input type="file" size="50" name="file1">
<span id="file1Progress">-</span>
<br/>
<a href="javascript:previewFile(1)">Preview</a>
<br/>
<br/>
<input type="file" size="50" name="file2">
<span id="file2Progress">-</span>
<br/>
<a href="javascript:previewFile(2)">Preview</a>
<br/>
<br/>
<input type="file" size="50" name="file3">
<span id="file3Progress">-</span>
<br/>
<a href="javascript:previewFile(3)">Preview</a>
<br/>
<br/>
<input type="file" size="50" name="file4">
<span id="file4Progress">-</span>
<br/>
<a href="javascript:previewFile(4)">Preview</a>
<br/>
<br/>
<input type="file" size="50" name="file5">
<span id="file5Progress">-</span>
<br/>
<a href="javascript:previewFile(5)">Preview</a>
<br/>
<br/>
<input type="button" value="Upload" id="submitButton" onclick="uploadForm.submit();doProgress();">
<br/>
<br/>
</form>
<div class="progBar">
File number: <span id="fileText">-</span> is being uploaded.<br/>
<br/>
<progress id="progressBar" value="0" max="100"></progress><br/>
Upload of all files is: <span id="progressText">-</span>% complete.<br/>
</div>
</div>
</body>
</html>