gpt4 book ai didi

java - 使用表单元素上传图像时出现错误

转载 作者:行者123 更新时间:2023-11-30 06:10:30 25 4
gpt4 key购买 nike

问题陈述:

我正在尝试上传图像以用于用户注册目的。

当我上传表单内没有任何表单元素的图像时。它将正常运行。

但是当我尝试使用表单内的元素上传图像时 会给我一个错误以获取更多信息,请参阅提供的代码和错误信息。

注册用户表格:

            <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 
Folder\UploadServlet30\build\web\uploadFiles (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
Folder\UploadServlet30\build\web\uploadFiles (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);

关于java - 使用表单元素上传图像时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50345697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com