我收到一個錯誤HTTP狀態500 - java.io.FileNotFoundException: C:\ SAM \ IMAGES(訪問被拒絕),同時保存圖像在文件'IMAGES'中。我嘗試了所有的事情,如更改文件的權限,並試圖將其保存在不同的文件夾中。奇怪的是,我的圖像實際上被存儲在我上面提到的文件中。但是爲什麼仍然它顯示錯誤?如何解決這個錯誤?Tomcat是用戶的所有權。java.io.FileNotFoundException(拒絕訪問)錯誤?但文件正在存儲
My Code:
MySampleUpload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FileUploadServlet" method="post"enctype="multipart/form-data">
<div class="btn btn-success btn-file">
<i class="fa fa-cloud-upload"></i>
Browse
<input type="file" name="file" />
</div>
<button type="submit" value="submit" name='submit'>submit</button>`
</form>
</body>
</html>
FileUploadServlet.java
import java.io.File;
import java.io.IOException;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.RandomAccessFile;
/**
* Servlet implementation class FileUploadServlet
*/
@WebServlet("/FileUploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*10, // 10 MB
maxFileSize=1024*1024*50, // 50 MB
maxRequestSize=1024*1024*100) // 100 MB
public class FileUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
private static final String UPLOAD_DIR="IMAGES";
public FileUploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//gets absolute path of the web application
//String applicationPath=request.getServletContext().getRealPath("");
//construct path of the directory to save the uploaded file
String UploadFilePath="C:/sam"+File.separator+UPLOAD_DIR;
//creates the file directory if it doesn't exist
File FileSaveDir=new File(UploadFilePath);
if(!FileSaveDir.exists())
{
FileSaveDir.mkdirs();
}
System.out.println("Upload file Diiirectory"+FileSaveDir.getAbsolutePath());
String fileName=null;
//get all parts from request and write it to the file on the server
for(Part part:request.getParts())
{
fileName=getFileName(part);
part.write(UploadFilePath + File.separator + fileName);
}
System.out.println("File Uploaded successfully");
request.setAttribute("message",fileName+"file uploaded successfully");
getServletContext().getRequestDispatcher("/response.jsp").forward(request, response);
}
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 "";
}}
而所有沒有任何代碼? – Axel
可能會添加一些代碼行,並顯示錯誤顯示的位置將有所幫助,以及提及哪個用戶是在at下運行的tomcat以及目錄的權限是什麼 – JChrist
您是否嘗試過調試它?上傳單個文件時請求中有多少部分?所有這些部分都包含'fileName'嗎?如果'getFileName'返回一個空字符串,你會怎麼想? – JChrist