java.io.FileNotFoundException:C:\Users\user\AppData\Local\Temp(访问被拒绝)
当我从Mozilla浏览器上传文件到本地系统的临时目录时,我遇到了拒绝访问错误。但是如果我在Eclipse浏览器中执行相同的操作,我看不到任何错误,这意味着它正在上传,没有任何错误: 代码: 错误:java.io.FileNotFoundException:C:\Users\user\AppData\Local\Temp(访问被拒绝),java,Java,当我从Mozilla浏览器上传文件到本地系统的临时目录时,我遇到了拒绝访问错误。但是如果我在Eclipse浏览器中执行相同的操作,我看不到任何错误,这意味着它正在上传,没有任何错误: 代码: 错误: java.io.IOException: java.io.FileNotFoundException: C:\Users\user\AppData\Local\Temp (Access is denied) 艾伦,这是密码: final String path = System.getProper
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();
}
}
对于web应用程序,Webcontainer可能设置了一些SecurityManager()来阻止对本地文件系统的写访问。
检查情况是否如此…请参见此示例,在创建文件时使用两个参数作为示例:
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();
}
}
}
参考资料:
几分钟前我遇到过同样的问题。 您的代码无法与其他请求参数一起用于文件上载 调用getParts()时,它还将其他参数作为部分
”;filename=“
”
如果从提交,则需要注意的事项可能不同
不同的浏览器。尝试从eclipse的内置浏览器提交它。
尝试按打印并查看内容处置标题
System.out.println(part.getHeader(“内容处置”)
但这也不是一个好的解决方案,因为循环迭代没有理由使用其他参数。请看,谢谢Allan提供的参考问题,但我的问题是,这是一个web应用程序,任何人都可以使用。任何人如何获得用户临时文件夹的访问权限(根据Sircapsalot的解决方案)。如果我的理解不正确,请告诉我。您可以尝试使用createTempDirectory.File myTempDir=Files.createTempDir()类在其他路径中创建;尝试使用“File myTempDir=Files.createTempDir()”时,我再次看到访问DeniedAllan时出现相同的错误:终于成功了。非常感谢您花时间和代码。艾伦:在临时文件夹中写入文件后,如何删除该文件。我创建了一个具有相同文件路径的文件对象,并尝试使用“fileobject.delete()”进行删除。但该文件未被删除。重新启动计算机时,该文件将自动删除。不,艾伦,这不会发生。即使在系统重新启动后,这些文件也不会被删除。在问题部分添加了代码。请查收。
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;
}