java - J2ee 图片上传错误:java.lang.IllegalStateException
问题描述
我正在尝试通过 jsp 表单将图像上传到数据库。但是有一个错误,
java.lang.IllegalStateException:在没有多部分配置的情况下调用 Request.getPart。将 @MultipartConfig 添加到 servlet,或将 multipart-config 元素添加到 web.xml
我的 servlet 代码;
/* image */
Part filePart = request.getPart("eImage"); //here is the error
InputStream inputStream = null;
if (filePart != null) {
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
但是当我添加时,
<multipart-config>
<location>/tmp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
作为子元素,根据https://docs.oracle.com/javaee/7/tutorial/servlets011.htm,但也会出现错误;
严重:部署应用程序时出现异常 [events_handeling]:org.xml.sax.SAXParseException;行号:12;列号:23;档案 [web] 中的部署描述符文件 WEB-INF/web.xml。cvc-complex-type.2.4.a:发现以元素“multipart-config”开头的无效内容。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<multipart-config>
<location>/tmp</location>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</web-app>
这应该如何解决?
解决方案
在标签内的 JSP 或 HTML 页面中写入 (enctype="multipart/form-data")。
<form name="form1" method="post" enctype="multipart/form-data" action="insertimage.jsp">
<input type="file" name="ImageFile" id="ImageFile" />
<input type="submit" name="submit" value="submit" />
</form>
用于从请求中读取文件的 Java 函数
try {
String ImageFile="", itemName = "";
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
}
catch (FileUploadException e) {
e.getMessage();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
if(name.equals("ImageFile")) {
ImageFile=value;
}
}
else {
try {
itemName = item.getName();
File savedFile = new File("config.getServletContext().getRealPath("/")+"Example\\image-folder\\"+itemName);
item.write(savedFile);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
catch (Exception e) {
out.println(e.getMessage());
}
从此代码中读取文件并保存到数据库中。
推荐阅读
- c# - 我们如何在 C# 中为 Azure Data Lake 在 azure fileclient 中设置 setMetadata
- python-3.x - 如果找到特定字符串,则设置为变量
- algorithm - 是否重新排列 Floyd-Warshall 算法中的外循环,因为大多数内循环会改变算法
- amazon-web-services - AWS 未能为放大应用程序创建后端
- r - 在 RSelenium 中截取屏幕截图
- reactjs - 反应改变状态下一个组件
- agora.io - Agora.io 用户无法显示其他用户,但可以发送自己的图像并显示
- api - 邮递员多次运行相同的请求
- microsoft-graph-api - 在 Teams 中跟踪用户位置
- python - matplotlib 中的工具栏在 tkinter 画布中不起作用