当我从Mozilla浏览器上传文件到本地系统的临时目录时,我得到访问被拒绝错误。但是,如果我从Eclipse Browser中做同样的事情,我没有看到任何错误,意味着它上传时没有任何错误:
代码:
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)
Allan,这是代码:
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();
}
}
答案 0 :(得分: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;
}
参考文献:
答案 1 :(得分:0)
对于Web应用程序,Web容器可能已设置一些SecurityManager(https://docs.oracle.com/javase/8/docs/api/java/lang/SecurityManager.html)来阻止对本地文件系统的写访问。 检查是否属于这种情况......
答案 2 :(得分:0)
几分钟前我遇到的同样问题。 您的代码无法与其他请求参数一起用于文件上传。
在调用 getParts()时,它将其他参数也作为一部分。
表单数据; name =“ <file-parameter-name>
”; filename =“ <filename>
”
<filename>
要注意的事项如果提交自 不同的浏览器。尝试从eclipse的内置浏览器中提交。 尝试打印并查看content-disposition标头 System.out.println(part.getHeader(“ content-disposition”));
表单数据;名称=“”
现在看到没有类似于filename =“”的东西,因此获取文件名的函数将返回null。
现在,您调用part.write(),但内部仅传递路径,而不传递文件名,因为调用获取文件名的函数返回null。因此,即使您实际上已上传文件,您也会遇到异常。
在获取文件名后放置一个条件
if(filename.equals(“”)){继续;} 但这也不是一个好的解决方案,因为无缘无故地循环遍历其他参数。