2011-06-27 5 views
0

J'utilise les 2 codes suivants pour télécharger un fichier en utilisant JSP .... mais je ne reçois aucune erreur, mais le fichier ne se télécharge pas .... savez-vous pourquoi ?Le fichier de téléchargement JSP ne fonctionne pas

S'il vous plaît aider

Merci 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

Oh my .. http://stackoverflow.com/questions/5038798/uploading-of-pdf-file/5041420#5041420 – BalusC

+0

@BalusC aïe ... – Bozho

Répondre

2
  1. Votre formulaire est manquant enctype="multipart/form-data"
  2. Il vous manque un <input type="file" /> donc je ne vois pas où serait le fichier venir de
  3. Utiliser commons-fileupload
  4. Ne pas mettre le code java dans JSP. Mettez-le dans une servlet.
+0

désolé oublié d'ajouter l'autre code, où le information arrive ... Documents Téléchargement

NOUVEAU As Built CHARGER

Sheeyla

+0

Ainsi, seuls les points 3 et 4 restent valables. – Bozho

+0

désolé de poser une telle question de base, mais je suis nouveau sur ce .... quand vous dites mettre java dans une servlet ... quel genre de changements dois-je apporter à ce code par exemple? – Sheeyla

Questions connexes