2011-06-27 7 views
0

JSPを使用してファイルをアップロードするのに、次の2つのコードを使用しています....しかし、エラーはありませんが、ファイルはアップロードされません。 ?JSPアップロードファイルが動作していません

あなたのフォームはあなたが<input type="file" />ファイルが

  • から来るどこので、私は表示されませんが欠落しているenctype="multipart/form-data"
  • が欠落している

    おかげ Sheeyla

    <%@ page import="java.io.*" %> 
    <html> 
    
    
    <head><title>Documents Upload</title></head> 
    
    <body bgcolor=#F5F5F5> 
    <% 
        //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 
         String folder = (String) new File(config.getServletContext().getRealPath("/")).getParent(); 
           folder= (String)folder.replace(File.separatorChar, '/'); 
           File savedFile = new File(folder +"/IngDemo/asbuilts/"+saveFile) ; 
         FileOutputStream fileOut = new FileOutputStream(folder + savedFile);  
         //FileOutputStream fileOut = new FileOutputStream(savedFile); 
         fileOut.write(dataBytes, startPos, (endPos - startPos)); 
         fileOut.flush(); 
         fileOut.close(); 
         %><br><table border="0"><tr><td><b>You have successfully upload the file by the name of:</b> 
         <% out.println(saveFile); %></td></tr></table> 
    
    <p><font size=2 face="Century Gothic"><b>NEW AS BUILT DATA TO ENTER</b></font></p> 
    <p><font size=1 color=red face="Century Gothic"><b>Note: Only enter data if this is an as built.<br> 
    If as built has been marked up, just re-upload.</b></font></p> 
    <form method=post action="CreateAs.jsp"> 
    <table> 
    <tr><td><font size= 2 face="Century Gothic">Document Title (with rev number):</font></td> 
    <td><input type=text name="d_title" size=50></td></tr> 
    <tr><td><font size=2 face"Century Gothic">Issued by :</font></td> 
    <td><input type=text name="i_by" size=30></td></tr> 
    <tr><td><font size=2 face="Century Gothic">Issued date:</font></td> 
    <td><input type=text name="i_date" size=20></td> 
    <td><input type="hidden" name="l_ocation" value="<%=saveFile%>"></td> 
    </tr> 
    </table> 
    <p><input type=submit value=Submit><p> 
    </form> 
    
    <% 
        } 
    %> 
    
    </body> 
    </html> 
    
  • +1

    私のああ...申し訳ありませんが、他のコードを追加するのを忘れhttp://stackoverflow.com/questions/5038798/uploading-of-pdf-file/5041420#5041420 – BalusC

    +0

    @BalusC痛い... – Bozho

    答えて

    2
    1. を助けてください使用commons-fileupload
    2. Do not Do JSPにJavaコードを入れてください。それをサーブレットに入れます。
    +0

    場所情報 ドキュメントがアップロード...来ている

    <フォントサイズ= 2の顔= "世紀のゴシック">NEWアップロードするために建てられたよう

    <フォームmethod = post enctype = mutipart/form-da名前= "uploaded_file" クラス= "ファイル">

    ​​の

    Sheeyla

    +0

    だから、唯一の3と4は有効のまま指します。 – Bozho

    +0

    このような基本的な質問を申し訳なく思っていますが、私はこれについて新しいものです。サーブレットにJavaを入れたら...このコードにどのような変更を加える必要がありますか? – Sheeyla

    関連する問題