Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 上载pdf文件_Java_Jsp_File Upload - Fatal编程技术网

Java 上载pdf文件

Java 上载pdf文件,java,jsp,file-upload,Java,Jsp,File Upload,我想使用下面给出的代码上传一个pdf文件。它提供了浏览功能,但不上传文件。当我单击sendfile按钮时,显示uploadfile.html代码页。我该怎么做??? 给定代码中的错误在哪里 filename-upload.html <%@ page language="java" %> <HTml> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>

我想使用下面给出的代码上传一个pdf文件。它提供了浏览功能,但不上传文件。当我单击sendfile按钮时,显示uploadfile.html代码页。我该怎么做??? 给定代码中的错误在哪里

filename-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> <%
        }
%>

= 0)) {
DataInputStream in=新的DataInputStream(请求。
getInputStream());
//我们正在计算内容类型数据的长度
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
//此循环将上载的文件转换为字节码
while(totalBytesRead
您已经成功 以以下名称上载文件:
这显然是一个代码片段。首先,它是一个学习资源。不要使用它。它只会教坏习惯。将该网站添加到你的黑名单中。事实上,任何“教程”网站上到处都是广告横幅和毫无希望地过时的低质量代码片段,显然是由业余爱好者维护的,他们主要关注的是广告收入,而不是严肃的教学。其他类似的“教程”例子网站有javabeat、TutorialPoint、journaldev、javatpoint等。这些网站的共同点是它们起源于印度

除了您错误地使用了
.html
文件扩展名而不是
.jsp
(即使他们使用
.jsp
扩展名正确地展示了他们的示例)之外,代码片段还有几个主要问题:

  • HTML使用的是'90年代风格的大写标记。不鼓励这样做
  • HTML使用了
    标记,这些标记自1998年以来就被弃用了
  • 业务逻辑与表示逻辑混合在一个JSP文件中。Java代码属于Java类,而不属于JSP文件
  • 解析器依赖于本身并不总是存在的
    内容长度
    请求头。如果不存在此头,则代码中断
  • 解析器正在创建该长度的字节数组。当内容长度大于可用服务器内存时,这可能会使服务器崩溃
  • 解析器正在基于字节数组创建
    字符串
    ,该字节数组使用服务器平台默认字符编码,而不是多部分头中指定的字符编码。这可能导致结果字节格式错误/损坏
  • DataInputStream
    包装器是不必要的,代码没有从中获益
  • 等等
  • 等等
太可怕了


从JSP上载文件的正确方法是将表单提交到一个
@MultipartConfig
带注释的servlet类,然后使用
request.getPart()
获取该文件。您可以在以下答案中找到一个片段:

以下答案详细阐述了学习Java EE的正确方法: