Reputation: 134
as the title implies I'm trying to upload a file to a local server and for that purpose I'm using a JSP and uploadify (flash-based jquery uploader). I have already succeeded on uploading a file using glassfish server 3.1 and this as my HTML code:
</head>
<body>
<table>
<tr>
<td>
<input id="file_upload" name="file_upload" type="file" />
<script type="text/javascript">
$('#file_upload').uploadify({
'uploader' : 'uploadify/uploadify.swf',
'script' : 'UploadFile/uploadFile.jsp',
'cancelImg' : 'uploadify/cancel.png',
'multi' : true,
'auto' : false,
'onComplete' : function(fileObj)
{
alert (fileObj.filePath); //The path on the server to the uploaded file
}
});
</script>
</td>
<td>
<a href="javascript:$('#file_upload').uploadifyUpload($('.uploadifyQueueItem').last().attr('id').replace('file_upload',''));">Upload Last File</a>
</td>
</tr>
</table>
</body>
And this as my server-side script:
<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<table>
<tr>
<td>
<b>File uploaded:</b>
<% out.println(saveFile);%>
</td>
</tr>
</table>
<%} else {
out.println(contentType.indexOf("multipart/form-data"));
out.println(request.getContentType());
%>
<br/> error <% } %>
So my question is, is it possible to change the default folder to upload things to? e.g: My default folder right now is C:\Users\USERNAME.netbeans\7.0\config\GF3\domain1 is it possible to change it to C:\Users\USERNAME\Desktop ?
If I was not clear enough with the question feel free to say it, any help is greatly appreciated, thanks.
Upvotes: 0
Views: 5052
Reputation: 76709
My default folder right now is C:\Users\USERNAME.netbeans\7.0\config\GF3\domain1 is it possible to change it to C:\Users\USERNAME\Desktop ?
Your default directory happens to be the domain1 directory as you are not specifying an absolute path to the file in the following line:
FileOutputStream fileOut = new FileOutputStream(saveFile);
Without an absolute path, the file will be saved in the location relative to the current working directory of the Java process, which in the case of the Glassfish application server, happens to be the domain directory. Once you specify an absolute path, you will be able to save the uploaded file to a location of your choice.
On a different note, consider the following points:
@MultipartConfig
annotation provided by the Servlet 3.0 API for handling the file upload requests; Tomcat 7 and Glassfish 3.1 rely on the well-written Apache Commons Fileupload library under the hood to process multi-part POST requests. That way, you wouldn't have to worry about processing the request yourself. You can instead retrieve the individual Parts yourself and leave the gruntwork to the container.Upvotes: 2