使用表单元素上载图像时出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用表单元素上载图像时出错相关的知识,希望对你有一定的参考价值。
问题陈述:
我正在尝试上传图像以供用户注册。
当我在表单中上传没有任何表单元素的图像时。它会正常运行。
但是,当我尝试使用表单内的元素上传图像时,它会给我一个错误,更多信息请参阅提供的代码和错误信息。
表格注册用户:
<form method="post" action="UploadServlet"
enctype="multipart/form-data">
username:<input type="text" name="username">
password:<input type="password" name="password">
city:<input type="text" name="city">
State:<input type="text" name="state">
Email:<input type="text" name="email">
Select file to upload: <input type="file" name="file" size="60" />
<br />
<br />
<input type="submit" value="Upload" />
</form>
Servlet的:
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
maxFileSize = 1024 * 1024 * 10, // 10MB
maxRequestSize = 1024 * 1024 * 50) // 50MB
public class UploadServlet extends HttpServlet {
private static final String SAVE_DIR = "uploadFiles";
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String appPath = request.getServletContext().getRealPath("");
String savePath = appPath + File.separator + SAVE_DIR;
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
for (Part part : request.getParts()) {
String fileName = extractFileName(part);
fileName = new File(fileName).getName();
part.write(savePath + File.separator + fileName);
}
request.setAttribute("message", "Upload has been done successfully!");
getServletContext().getRequestDispatcher("/userlist.jsp").forward(
request, response);
}
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 "";
}}
错误:
java.io.IOException: java.io.FileNotFoundException: E:New
FolderUploadServlet30uildwebuploadFiles (Access is denied)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.io.FileNotFoundException: E:New
FolderUploadServlet30uildwebuploadFiles (Access is denied)
java.io.FileOutputStream.open0(Native Method)
java.io.FileOutputStream.open(FileOutputStream.java:270)
java.io.FileOutputStream.<init>(FileOutputStream.java:213)
java.io.FileOutputStream.<init>(FileOutputStream.java:162)
org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.write(DiskFileItem. java:378)
net.codejava.servlet.UploadServlet.doPost(UploadServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
答案
在您的servlet中,doPost方法首先使用request.getParameter(“inputname”)检索所有参数。
之后,而不是使用foreach循环。只写
Part part = request.getPart("file");
String fileName = extractFileName(part);
part.write(savePath + File.separator + fileName);
以上是关于使用表单元素上载图像时出错的主要内容,如果未能解决你的问题,请参考以下文章