0

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.

J. Herrera
  • 134
  • 4
  • 9
  • 1
    This code is recognizeable as one of the roseindia.net examples. Please read this http://stackoverflow.com/questions/5038798/uploading-of-pdf-file/5041420#5041420. I stress you to drop this terrible piece of code altogether and go for Commons FileUpload or Servlet 3.0 `getParts()`. See also http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824 and http://stackoverflow.com/questions/2272160/uploadify-plugin-doesnt-call-java-servlet/2273711#2273711 – BalusC Jul 21 '11 at 14:22

1 Answers1

2

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:

  • use a Servlet to process the file upload request. JSPs are typically meant for generating presentation content for a view.
  • use the @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.
Vineet Reynolds
  • 72,899
  • 16
  • 143
  • 173
  • Thanks for the answer! So, what you are saying is that I should define the path as FileOutputStream fileOut = new FileOutputStream("C:\\Users\\USERNAME\\Desktop\\uploads"); This is king of confusing as this is my first time working with jsps and servlets. – J. Herrera Jul 20 '11 at 04:32
  • 1
    No, the path should be concatenated with `"C:\\Users\\USERNAME\\Desktop\\uploads"` so that you get the absolute file path. It is not the directory path that needs to be provided to the `FileOutputStream` constructor, but the absolute file path. – Vineet Reynolds Jul 20 '11 at 04:43
  • Just to add since your are new ,use 'request.getSession().getServletContext().getRealPath("/")' to retrieve the path. – zawhtut Jul 20 '11 at 15:14
  • @zawhtut: no, don't do that. You don't want to save uploaded files in expanded WAR folder. – BalusC Jul 21 '11 at 14:20
  • @BalusC Yea, which is true that you pointed out. The upload folder path should better be in the properties file or in the database. – zawhtut Jul 21 '11 at 15:01