【问题标题】:HTTP Status 404 Error when Running Upload File Code运行上传文件代码时出现 HTTP 状态 404 错误
【发布时间】:2015-02-11 12:46:05
【问题描述】:

我在单击提交按钮时收到错误“HTTP 状态 404 - 未找到”。给出的路径似乎是正确的。这段代码有什么会导致该错误发生吗?

<form name="uploadForm" action="index.jsp" method="POST" enctype="multipart/form-data">
        <%
            String saveFile="";
            String contentType = request.getContentType();

            if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){

                DataInputStream in = new DataInputStream(request.getInputStream());
                int formDataLength = request.getContentLength();
                byte dataBytes[] = new byte[formDataLength];
                int byteRead = 0;
                int totalBytesRead = 0;

                while(totalBytesRead < formDataLength){
                    byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
                    totalBytesRead += byteRead;
                }

                String file = new String(dataBytes);
                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;
                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;

                File ff = new File("C:/Users/JBG/Desktop/SampleFile2.docx"+saveFile);

                try{
                    FileOutputStream fileOut = new FileOutputStream(ff);
                    fileOut.write(dataBytes, startPos, (endPos - startPos));
                    fileOut.flush();
                    fileOut.close();}catch(Exception e){out.println(e);}
            }
        %>

        <input type="file" name="file" value="" />
        <input type="submit" value="Submit" name="submit" />
    </form>

【问题讨论】:

  • HTTP 代码 404 表示操作路径有问题。代码似乎很好。
  • @Daron 请分享完整的错误/异常堆栈跟踪。
  • 完整的错误/异常堆栈跟踪如下:严重:PWC6117:找不到文件“C:\Users\JBG\Documents\NetBeansProjects\ReadFileProgram\build\web\index.jsp”
  • 我的 index.jsp 文件位于以下位置。 (C:\Users\JBG\Documents\NetBeansProjects\ReadFileProgram\web\WEB-INF\jsp\index.jsp) 我该怎么办?

标签: java jsp upload


【解决方案1】:

当您收到 404 错误消息时。此错误消息表明

The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested.

The web site hosting server will typically generate a "404 Not Found" web page when a user attempts to follow a broken or dead link; hence the 404 error is one of the most recognizable errors users can find on the web.[1]

所以在文件操作正确的情况下有一个小错误。

在您的代码中

File ff = new File("C:/Users/JBG/Desktop/SampleFile2.docx"+saveFile);

替换下面的语法。

File ff = new File("C:/Users/JBG/Desktop/directory_name/"+saveFile);

File ff = new File("C:/Users/JBG/Desktop/"+saveFile);

【讨论】:

  • 您使用哪种 ide 开发应用程序?您也使用错误的路径来访问 jsp 文件。如果你正在使用任何 IDE,那么请确保你所有的 jsp/html 文件都应该在 web pages 目录中。
【解决方案2】:

你的动作标签应该指向一个有效的端点 servlet /webservice 。看起来您将其指向一个 jsp 文件。也就是说,混合表示逻辑和服务器端逻辑是在 2000 年代初期完成的。在过去的十年里,情况有了很大的改善。我的建议是查看 Jersey + Twitter Bootstrap 甚至 Play 来实现你正在做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 2015-08-04
    • 2020-09-01
    • 1970-01-01
    • 2015-11-23
    • 2018-09-13
    • 2021-01-05
    • 2017-02-24
    相关资源
    最近更新 更多