2011-07-20 49 views
0

作爲標題暗示我試圖上傳文件到本地服務器,爲此我使用JSP和uploadify(flash基於jquery的上傳器)。 我已經成功地使用GlassFish服務器3.1上傳文件,並以此作爲我的HTML代碼:使用jsp作爲服務器端腳本將文件上傳到特定文件夾

</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> 

並將此作爲我的服務器端腳本:

<%@ 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 <% } %> 

所以我的問題是,它可以更改默認文件夾上傳的東西? 例如:我現在的默認文件夾是C:\ Users \ USERNAME.netbeans \ 7.0 \ config \ GF3 \ domain1是否可以將其更改爲C:\ Users \ USERNAME \ Desktop?

如果我沒有足夠清楚的問題隨意說出來,任何幫助非常感謝,謝謝。

+1

此代碼可識別爲roseindia.net示例之一。請閱讀此http://stackoverflow.com/questions/5038798/uploading-of-pdf-file/5041420#5041420。我強調你完全放棄這段可怕的代碼,然後去Commons FileUpload或Servlet 3.0'getParts()'。另見http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824和http://stackoverflow.com/questions/2272160/uploadify-plugin-doesnt- call-java-servlet/2273711#2273711 – BalusC

回答

2

現在我的默認文件夾是C:\ Users \ USERNAME.netbeans \ 7.0 \ config \ GF3 \ domain1是否可以將其更改爲C:\ Users \ USERNAME \ Desktop?

你的默認目錄恰好是DOMAIN1目錄你不指定一個絕對路徑,以在以下行的文件:

FileOutputStream fileOut = new FileOutputStream(saveFile); 

沒有一個絕對路徑,文件將被保存在相對於Java進程當前工作目錄的位置,在Glassfish應用程序服務器的情況下,恰好是域目錄。一旦你指定了絕對路徑,你將能夠將上傳的文件保存到你選擇的位置。


在不同的音符,請考慮以下幾點:

  • 使用servlet處理文件上傳請求。 JSP通常用於爲視圖生成演示內容。
  • 使用Servlet 3.0 API提供的@MultipartConfig註釋來處理文件上傳請求; Tomcat 7和Glassfish 3.1依賴於引擎蓋下良好編寫的Apache Commons Fileupload庫來處理多部分POST請求。這樣,您就不必擔心自己處理請求。您也可以自己檢索各個部件,並將gruntwork留給容器。
+0

感謝您的回答!所以,你所說的是我應該將路徑定義爲FileOutputStream fileOut = new FileOutputStream(「C:\\ Users \\ USERNAME \\ Desktop \\ uploads」);這是令人困惑的王者,因爲這是我第一次使用jsps和servlet。 –

+1

不,路徑應該與'「C:\\ Users \\ USERNAME \\ Desktop \\ uploads」'連接在一起,以便獲得絕對文件路徑。它不是需要提供給'FileOutputStream'構造函數的目錄路徑,而是絕對文件路徑。 –

+0

只需添加,因爲你是新的,使用'request.getSession()。getServletContext()。getRealPath(「/」)'檢索路徑。 – zawhtut

相關問題