开发者

JSP upload file is not working

I am using the following 2 codes to upload a file using JSP.... but I don´t get any error, but the file does not get uploaded.... do you know why?

Please help

Thanks 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>
<t开发者_JAVA百科r><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. Your form is missing enctype="multipart/form-data"
  2. You are missing a <input type="file" /> so I don't see where would the file come from
  3. Use commons-fileupload
  4. Don't put java code in JSP. Put it in a servlet.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜