任何人都可以帮助解决错误吗? 尝试在服务器文件夹中上传多个文件,文件名在数据库中,但是服务器抛出以下错误-
HTTP状态500
类型例外报告
信息
描述服务器遇到内部错误(),导致服务器无法满足此请求。
例外
java.io.IOException:java.io.FileNotFoundException:F:\\ eworkspace.metadata.plugins \\ org.eclipse.wst.server.core \\ tmp0 \\ wtpwebapps \\ UploadServlet30 \\ uploadFiles(拒绝访问)net.codejava.servlet.UploadServlet .doPost(UploadServlet.java:53)javax.servlet.http.HttpServlet.service(HttpServlet.java:641)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)根本原因
java.io.FileNotFoundException:F:\\ eworkspace.metadata.plugins \\ org.eclipse.wst.server.core \\ tmp0 \\ wtpwebapps \\ UploadServlet30 \\ uploadFiles(拒绝访问)java.io.FileOutputStream.open(本机方法)java。 io.FileOutputStream。(未知源)java.io.FileOutputStream。(未知源)org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem.java:425)net.codejava.servlet.UploadServlet。 doPost(UploadServlet.java:53)javax.servlet.http.HttpServlet.service(HttpServlet.java:641)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
控制器类
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
News news = new News();
// gets absolute path of the web application
String appPath = request.getServletContext().getRealPath("");
// constructs path of the directory to save uploaded file
String savePath = appPath + File.separator + UPLOAD_DIR;
// creates the save directory if it does not exists
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
for (Part part : request.getParts()) {
System.out.println("partttttttttt"+part.getName());
String fileName = extractFileName(part);
System.out.println("fle Name-----"+fileName);
// refines the fileName in case it is an absolute path
fileName = new File(fileName).getName();
System.out.println("fle Name-----"+fileName);
part.write(savePath + File.separator + fileName);
news.setFileName(fileName);
}
news.setNewsTitle(request.getParameter("newsTitle"));
news.setNewsDesc(request.getParameter("newsDesc"));
String newsId = request.getParameter("newsId");
if(newsId == null || newsId.isEmpty())
{
dao.addNews(news);
}
else
{
news.setNewsId(Integer.parseInt(newsId));
dao.updateNews(news);
}
RequestDispatcher view = request.getRequestDispatcher(LIST_NEWS);
request.setAttribute("newsList", dao.getAllNews());
view.forward(request, response);
}
/**
* Extracts file name from HTTP header content-disposition
*/
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}
和道
public void addNews(News news) {
String insertQuery = "INSERT INTO NEWSMASTER(NEWSID, NEWSTITLE, NEWSDESC,FILENAME) VALUES (?,?,?,?)";
try {
pStmt = dbConnection.prepareStatement(insertQuery);
pStmt.setInt(1, news.getNewsId());
pStmt.setString(2, news.getNewsTitle());
pStmt.setString(3, news.getNewsDesc());
pStmt.setString(4, news.getFileName());
pStmt.executeUpdate();
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
模型类
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}