【问题标题】:java.io.FileNotFoundException: C:\Users\user\AppData\Local\Temp (Access is denied)java.io.FileNotFoundException: C:\Users\user\AppData\Local\Temp(访问被拒绝)
【发布时间】:2018-06-14 12:35:18
【问题描述】:

当我从 Mozilla 浏览器将文件上传到本地系统的临时目录时,我收到拒绝访问错误。但是如果我从 Eclipse 浏览器做同样的事情,我没有看到任何错误,这意味着它正在上传而没有任何错误:

代码:

for (Part part : request.getParts()) {
     fileName = getFileName(part);
     part.write(System.getProperty("java.io.tmpdir") + fileName);
}


private String getFileName(Part part) {
    String contentDisp = part.getHeader("content-disposition");
    System.out.println("content-disposition header= "+contentDisp);
    String[] tokens = contentDisp.split(";");
    for (String token : tokens) {
        if (token.trim().startsWith("filename")) {
            return token.substring(token.indexOf("=") + 2, token.length()-1);
        }
    }
    return "";

错误:

java.io.IOException: java.io.FileNotFoundException: C:\Users\user\AppData\Local\Temp (Access is denied)

艾伦,这是代码:

final String path = System.getProperty("java.io.tmpdir");
OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter();

    try {

        for (Part part : request.getParts()) {
            String fileName = getFileName(part);

            out = new FileOutputStream(new File(path , fileName));
            filecontent = part.getInputStream();

            int read = 0;
            final byte[] bytes = new byte[1024];

            while ((read = filecontent.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            File UploadedFile = new File(path + File.separator + fileName);
            UploadedFile.delete();
       }

    } catch (FileNotFoundException fne) {

        writer.println("You either did not specify a file to upload or are "
                + "trying to upload a file to a protected or nonexistent "
                + "location.");


    } finally {
        if (out != null) {
            out.close();
        }
        if (filecontent != null) {
            filecontent.close();
        }
        if (writer != null) {
            writer.close();
        }
    }

【问题讨论】:

  • 感谢 Allan 提供参考问题,但我的问题是,它是一个网络应用程序,因此任何人都可以使用。任何人如何获得对用户临时文件夹的访问权限(根据 Sircapsalot 的解决方案)。如果我的理解不正确,请告诉我。
  • 您可以尝试使用 createTempDirectory 类在另一个路径中创建。
  • 文件 myTempDir = Files.createTempDir();
  • 尝试使用“File myTempDir = Files.createTempDir()”,我再次看到同样的错误访问被拒绝

标签: java


【解决方案1】:

看这个例子,创建文件时使用两个参数作为例子:

File scratchFile = new File(System.getProperty("java.io.tmpdir"), "filename.tmp");

例子:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    // Create path components to save the file
    final String path = System.getProperty("java.io.tmpdir");
    final Part filePart = request.getPart("file");
    final String fileName = getFileName(filePart);

    OutputStream out = null;
    InputStream filecontent = null;
    final PrintWriter writer = response.getWriter();

    try {
        //File Temp here with two parameters
        out = new FileOutputStream(new File(path , "filename.tmp"));

        filecontent = filePart.getInputStream();

        int read = 0;
        final byte[] bytes = new byte[1024];

        while ((read = filecontent.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        writer.println("New file " + fileName + " created at " + path);


    } catch (FileNotFoundException fne) {

        writer.println("You either did not specify a file to upload or are "
                + "trying to upload a file to a protected or nonexistent "
                + "location.");
        writer.println("<br/> ERROR: " + fne.getMessage());

    } finally {
        if (out != null) {
            out.close();
        }
        if (filecontent != null) {
            filecontent.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

还有你的方法:

private String getFileName(final Part part) {
      final String partHeader = part.getHeader("content-disposition");
      LOGGER.log(Level.INFO, "Part Header = {0}", partHeader);
      for (String content : part.getHeader("content-disposition").split(";")) {
          if (content.trim().startsWith("filename")) {
              return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
          }
      }
      return null;
}

参考资料:

Permission

Upload Method

【讨论】:

  • 艾伦:终于成功了。非常感谢您抽出宝贵时间并提供该代码。
  • Allan:在临时文件夹中写入文件后如何删除该文件。我创建了一个具有相同文件路径的文件对象,并尝试使用“fileobject.delete()”进行删除。但是文件没有被删除。
  • 重启电脑后文件会自动删除。
  • 不,艾伦,它没有发生。系统重启后文件也不会被删除。
  • 在问题部分添加了代码。请检查。
【解决方案2】:

对于 Web 应用程序,Webcontainer 可能设置了一些 SecurityManager (https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html) 来阻止对本地文件系统的写访问。 检查是否是这种情况...

【讨论】:

    【解决方案3】:

    几分钟前我遇到了同样的问题。 您的代码不适用于与其他请求参数一起上传的文件。

    当您调用 getParts() 时,它也将其他参数作为部分。

    1. 现在如果将文件作为部分内容-dipsosition 标头具有

    表格数据;名称="&lt;file-parameter-name&gt;";文件名="&lt;filename&gt;"

    需要注意的一点&lt;filename&gt; 如果从 不同的浏览器。尝试从 eclipse 的内置浏览器中提交它。 尝试打印并查看 content-disposition 标题 System.out.println(part.getHeader("content-disposition"));

    1. 如果您的循环针对作为一部分的其他参数运行,则 content-disposition 具有

    表格数据;名称=""

    现在看到没有像 filename="" 这样的东西,所以你获取文件名的函数返回 null。

    现在您调用 part.write() 但内部仅传递路径而不是文件名,因为您调用获取文件名的函数返回 null。因此,即使它实际上上传了文件,也会出现异常。

    获取文件名后设置条件

    if(filename.equals("")){继续;} 但这也不是一个好的解决方案,因为循环迭代没有其他参数的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2015-08-14
      • 1970-01-01
      相关资源
      最近更新 更多