【问题标题】:Upload file Servlet on hardcoded server path在硬编码的服务器路径上上传文件 Servlet
【发布时间】:2013-04-16 08:07:50
【问题描述】:

我有一个 .jsp 页面,其中显示了一个文件浏览器和一个上传按钮以及一个应该将文件上传到服务器的 Servlet。问题是文件上传到一个临时文件夹,但我需要将它上传到服务器上的特定路径,但路径无法识别。

UploadServlet:doPost 方法

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        // get access to file that is uploaded from client
        Part p1 = request.getPart("file");
        InputStream is = p1.getInputStream();

        String filename = getFilename(p1);

        // get temporary path - this works but it saves on a tmp folder...
        //String outputfile = this.getServletContext().getRealPath(filename);  // get path on the server

        //hardcoded path on which I have to save my file
        String outputfile = "http://localhost:8080/Tutorial2/Upload/" + filename;

        FileOutputStream os = new FileOutputStream (outputfile);

        // write bytes taken from uploaded file to target file
        int ch = is.read();
        while (ch != -1) {
             os.write(ch);
             ch = is.read();
        }
        os.close();
        out.println("<h3>File uploaded successfully!</h3>");
    }
    catch(Exception ex) {
       out.println("Exception -->" + ex.getMessage());
    }
    finally { 
        out.close();
    }
}

private static String getFilename(Part part) {
    for (String cd : part.getHeader("content-disposition").split(";")) {
        if (cd.trim().startsWith("filename")) {
            String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
            return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
        }
    }
    return null;
}

.jsp 页面

  <form action="UploadServlet" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>Select File : </td>
            <td><input  name="file" type="file"/> </td>
        </tr>
    </table>
    <p/>
    <input type="submit" value="Upload File"/>
</form>

我看过很多关于这个问题的帖子,但都没有帮助我。当我运行我的 servlet 时,我在 FileOutputStream os = new FileOutputStream (outputfile); - Exception --&gt;http:\localhost:8080\Tutorial2\Upload\SvnBridge.exe (The filename, directory name, or volume label syntax is incorrect) 得到一个异常。搜索了这个,没有找到任何相关的东西。我不知道它是否有帮助,但我使用 Servlet 3.0 和 Apache Tomcat v7。

你知道为什么我不能将我的文件写入上面的路径吗?

谢谢,

【问题讨论】:

    标签: java tomcat servlets fileoutputstream


    【解决方案1】:

    类似的问题和答案 How to set the path to "context path" for uploaded files using Apache Common fileupload?

    使用 java.io.File

    String relativeWebPath = "/WEB-INF/uploads";
    String absoluteFilePath = getServletContext().getRealPath(relativeWebPath);
    File uploadedFile = new File(absoluteFilePath, FilenameUtils.getName(item.getName()));
    // ...
    

    【讨论】:

    • 我记得通过上面的链接,但我认为当时我的 servlet 有其他结构......无论如何......非常感谢......它完美地工作......但我必须说我期待文件要复制到我的 Eclipse 工作区项目文件夹中,但它被复制到 'D:\workspaceEE\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\Tutorial2\Upload\',但它是好的,只要我可以使用 'localhost:8080/Tutorial2/Upload/[name of file]' 访问它。再次感谢!
    【解决方案2】:

    通过查看异常,表明问题是由于路径配置不正确。
    所以你可以使用

    File targetFile = new File(targetPath, filename) ;  // target path is hard coded for you.
    

    因为这样会更加注意路径。

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 2016-08-21
      • 2014-02-11
      • 2021-09-11
      • 2011-06-28
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多