gpt4 book ai didi

java - java(struts)中如何使用jsp上传多张图片?

转载 作者:行者123 更新时间:2023-12-02 09:26:19 24 4
gpt4 key购买 nike

我有jsp代码

college.jsp页面

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html>
<head>
</head>
<body>
<html:form action ="/college.do">
<fieldset>
<legend>COLLEGE INFORMATION :</legend>
<pre>
Gallery Images: <input type="file" name="file[]" multiple/>
<html:submit value = "S U B M I T"/>
</fieldset>
</html:form>
</body>
</html>

如何在struts中上传多张图片

项目中存储文件夹的图像总数

使用这个jsp,但我想要 CollegeAction 类和 CollegeForm 类如何做到这一点请帮我编码

最佳答案

你可能想做这样的事情。在您的 jsp 文件中

<html:file property="image1"/>
<html:file property="image2"/>
<html:file property="image3"/>

不要忘记设置 html 表单的属性 enctype="multipart/form-data"

然后在您的 Form 文件中创建图像变量:

private FormFile image1;
private FormFile image2;
private FormFile image3;

..以及它们的 getter 和 setter。

然后您可以使用这些变量在服务器端创建图像:

OutputStream bos = null;
InputStream stream = null;
try {
String fileName = form.getImage().getFileName();
String directory = "C:/your_folder";
File f = new File(directory);
if (!f.exists()) {
f.mkdir();

if (!"".equals(fileName)) {
stream = form.getImage1().getInputStream();
bos = new FileOutputStream(directory + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];

while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

关于java - java(struts)中如何使用jsp上传多张图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12103702/

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