JSP上传文件不起作用

时间:2011-06-27 17:22:29

标签: jsp file-upload

我使用以下2个代码使用JSP上传文件....但我没有收到任何错误,但文件没有上传....你知道为什么吗?

请帮忙

由于 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 个答案:

答案 0 :(得分:2)

  1. 您的表单遗失enctype="multipart/form-data"
  2. 您错过了<input type="file" />所以我看不到文件来自哪里
  3. 使用commons-fileupload
  4. 不要将java代码放在JSP中。把它放在一个servlet中。