2011-02-18 85 views
1

我想通過使用下面給出的代碼上傳PDF文件。它給瀏覽設備,但不上傳文件。當我點擊sendfile按鈕時它顯示uploadfile.html代碼頁。我怎樣才能做到這一點??? 哪裏是給定的代碼錯誤?pdf文件的上傳

文件名-upload.html

<%@ page language="java" %> 
<HTml> 
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> 
<% // for uploading the file we used Encrypt type of multipart/ 
form-data and input of file type to browse and submit the file %> 
    <BODY> <FORM ENCTYPE="multipart/form-data" ACTION= 
"uploadfile.html" METHOD=POST> 
     <br><br><br> 
     <center><table border="2" > 
        <tr><center><td colspan="2"><p align= 
"center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr> 
        <tr><td><b>Choose the file To Upload:</b> 
</td> 
        <td><INPUT NAME="F1" TYPE="file"></td></tr> 
        <tr><td colspan="2"> 
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr> 
      <table> 
    </center>  
    </FORM> 
</BODY> 
</HTML> 

文件名 - uploadfile.html

<%@ page import="java.io.*" %> 
<% 
    //to get the content type information from JSP Request Header 
    String contentType = request.getContentType(); 
    //here we are checking the content type is not equal to Null and 
as well as the passed data from mulitpart/form-data is greater than or 
equal to 0 
    if ((contentType != null) && (contentType.indexOf("multipart/ 
form-data") >= 0)) { 
     DataInputStream in = new DataInputStream(request. 
getInputStream()); 
     //we are taking the length of Content type data 
     int formDataLength = request.getContentLength(); 
     byte dataBytes[] = new byte[formDataLength]; 
     int byteRead = 0; 
     int totalBytesRead = 0; 
     //this loop converting the uploaded file into byte code 
     while (totalBytesRead < formDataLength) { 
      byteRead = in.read(dataBytes, totalBytesRead, 
formDataLength); 
      totalBytesRead += byteRead; 
      } 

     String file = new String(dataBytes); 
     //for saving the file name 
     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; 
     //extracting the index of file 
     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; 

     // creating a new file with the same name and writing the 
content in new file 
     FileOutputStream fileOut = new FileOutputStream(saveFile); 
     fileOut.write(dataBytes, startPos, (endPos - startPos)); 
     fileOut.flush(); 
     fileOut.close(); 

     %><Br><table border="2"><tr><td><b>You have successfully 
upload the file by the name of:</b> 
     <% out.println(saveFile); %></td></tr></table> <% 
     } 
%> 

回答

34

這顯然是一個Roseindia代碼段。首先,它是有史以來的worst學習資源。不要使用它。它只教壞習慣。將該網站添加到您的黑名單。事實上,任何充斥着廣告橫幅且無望地過時低質量代碼片段的「教程」網站明顯由業餘愛好者維護,主要關注廣告收入,而不是嚴肅的教學。這種廢話「教程」網站的其他例子是javabeat,tutorialspoint,journaldev,javatpoint等。這些網站所具有的顯着的共同點是它們起源於印度。

除了事實上,你正確使用.html文件擴展名,而不是.jsp(即使他們展示了他們的例子正確地.jsp擴展),有與代碼段幾大問題:

  • 的HTML是使用90年代風格的大寫標籤。這是不鼓勵的。
  • 的HTML使用<font><center>標籤被廢棄了,因爲1998年
  • 業務邏輯,在一個單一的JSP文件的顯示邏輯混雜。 Java代碼屬於Java類,不屬於JSP文件。
  • 解析器依賴於Content-Length請求標頭,它本身並不總是存在。如果此標題不存在,代碼會中斷。
  • 解析器正在創建該長度的字節數組。當內容長度大於可用服務器內存時,這可能會導致服務器崩潰。
  • 解析器基於字節數組使用服務器平臺默認字符編碼創建String,而不是在多部分頭中指定的字符數組。這可能會造成/損壞結果字節。
  • DataInputStream包裝是不必要的,代碼沒有任何好處。
  • 等。
  • 等。

這簡直太可怕了。


上傳從JSP文件的正確方法是將表格提交到@MultipartConfig註釋的servlet類,然後使用request.getPart()來獲取文件。您可以在此找到答案的一個片段:How to upload files to server using JSP/Servlet?

學習Java EE正確的方法是在這個答案闡述:Java EE web development, where do I start and what skills do I need?

+0

與Roseindia事情完全同意。不 - 不學習一些東西。 – navaltiger 2014-11-12 06:48:00