当前位置:首页 » 文件管理 » struts多文件上传

struts多文件上传

发布时间: 2023-01-04 05:39:10

A. 怎么用struts2实现多图片上传

新建Web Project,在WebRoot下新建upload文件夹
在WebRoot下新建upload.jsp,上传界面

编写上传成功、失败的提示界面。
在WebRoot下新建uploadError.jsp

在WebRoot下新建 uploadSuccess.jsp

编写Action类

配置struts.xml文件,重置fileUpload拦截器。

测试,测试完成之后在tomcat下面webapps目录下找到项目对应的文件夹下的upload下查看

B. struts上传多张图片怎么做,要求可以说上传中文的,而且图片可以重复上传不被覆盖

你指的是一次上传多张吗?我这有个自己写的例子:
//上传实体类
public class Upload {
private File photoes; //封装上传文件的属性
private String photoesFileName; //封装上传文件的名称属性
private String photoesContentType; //封装上传文件的类型属性
private String targetdir; //保存路径
private String targetfilename; //保存的文件名

public File getPhotoes() {
return photoes;
}
public void setPhotoes(File photoes) {
this.photoes = photoes;
}
public String getPhotoesFileName() {
return photoesFileName;
}
public void setPhotoesFileName(String photoesFileName) {
this.photoesFileName = photoesFileName;
}
public String getPhotoesContentType() {
return photoesContentType;
}
public void setPhotoesContentType(String photoesContentType) {
this.photoesContentType = photoesContentType;
}
public String getTargetdir() {
return targetdir;
}
public void setTargetdir(String targetdir) {
this.targetdir = targetdir;
}
public String getTargetfilename() {
return targetfilename;
}
public void setTargetfilename(String targetfilename) {
this.targetfilename = targetfilename;
}
}
这是上传图片的Action类中的部分代码
//获得服务器上保存上传文件的目录images的绝对路径
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
//设置保存文件的目录
uld.setTargetdir(realpath);
//设置目标文件名
uld.setTargetfilename(generateFileName(uld.getPhotoesFileName()));
//把doc内容复制到target
FileUtils.File(uld.getPhotoes(), new File(uld.getTargetdir(),uld.getTargetfilename()));

C. java中怎么利用struts2上传多个pdf文件

通过3种方式模拟多个文件上传
第一种方式
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
private File[] image; //上传的文件
private String[] imageFileName; //文件名称
private String[] imageContentType; //文件类型
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (image != null) {
File savedir=new File(realpath);
if(!savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();
for(int i=0;i<image.length;i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.File(image[i], savefile);
}
ActionContext.getContext().put("message", "文件上传成功");
}
return "success";
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
}
第二种方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用数组上传多个文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport{
private File[] image; //上传的文件
private String[] imageFileName; //文件名称
private String[] imageContentType; //文件类型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
//取得需要上传的文件数组
File[] files = getImage();
if (files !=null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
//建立上传文件的输出流, getImageFileName()[i]
System.out.println(getSavePath() + "\\" + getImageFileName()[i]);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]);
//建立上传文件的输入流
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len=fis.read(buffer))>0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
}
return SUCCESS;
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
第三种方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用List上传多个文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction3 extends ActionSupport {
private List<File> image; // 上传的文件
private List<String> imageFileName; // 文件名称
private List<String> imageContentType; // 文件类型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
// 取得需要上传的文件数组
List<File> files = getImage();
if (files != null && files.size() > 0) {
for (int i = 0; i < files.size(); i++) {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName().get(i));
FileInputStream fis = new FileInputStream(files.get(i));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
return SUCCESS;
}
public List<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="upload" namespace="/upload" extends="struts-default">
<action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload1" namespace="/upload1" extends="struts-default">
<action name="upload1" class="com.ljq.action.UploadAction2" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload2" namespace="/upload2" extends="struts-default">
<action name="upload2" class="com.ljq.action.UploadAction3" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
上传表单页面upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
<!-- ${pageContext.request.contextPath}/upload1/upload1.do -->
<!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
<!-- -->
<form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上传" />
</form>
</body>
</html>
显示页面message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
上传成功
<br/>
<s:debug></s:debug>
</body>
</html>

D. 用struts上传多个文件的问题!

我知道你说的意思了 你的意思就是网络空间那种上传呗?
只需一个Form
你可以这样 把上传那个文本框都起一样的名 比如text
然后在struts里接收
Enumeration e=request.getParameterNames("text");
while(e.hasMoreElements()){
String path= e.nextElement()+"";//获得每一个路径
}

E. struts2 在jsp一个上传框如何实现全选多个文件一次上传呢

jsp代码
<s:form action="uploadAction!uploadFilemuch.action" method="post" enctype="multipart/form-data" >

File1:<s:file name="uploadmuch" label="selectfile"></s:file>
File2:<s:file name="uploadmuch" label="selectfile"></s:file>
File3:<s:file name="uploadmuch" label="selectfile"></s:file>
<s:submit value="submit" />

</s:form>
action代码
public class UpLoadFiled extends ActionSupport {

private File upload;
private String uploadFileName;

private File[] uploadmuch;
private String[] uploadmuchFileName;

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public File[] getUploadmuch() {
return uploadmuch;
}

public void setUploadmuch(File[] uploadmuch) {
this.uploadmuch = uploadmuch;
}

public String[] getUploadmuchFileName() {
return uploadmuchFileName;
}

public void setUploadmuchFileName(String[] uploadmuchFileName) {
this.uploadmuchFileName = uploadmuchFileName;
}

public String uploadFilemuch() throws IOException {
if (this.getUploadmuch() != null) {
for (int i = 0; i < this.getUploadmuch().length; i++) {
FileOutputStream fos = new FileOutputStream(
ServletActionContext.getRequest().getRealPath(
"\\upload")
+ "\\" + this.getUploadmuchFileName()[i]);
FileInputStream fis = new FileInputStream(this.getUploadmuch()[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
ActionContext.getContext().put("message", "上传成功");
Log.addLog("上传成功");
return "message";
}

}

F. struts2.0怎么实现上传文件

一、创建jsp页面:
注意!要上传文件,表单必须添加 enctype 属性,如下: enctype="multipart/form-data"
index.jsp 代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 注意!表单必须添加 enctype 属性,值为"multipart/form-data" -->
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传"/>
</form>
</body>
</html>

二、创建Action类:
1. 添加三个私有字段,并添加相应的get,set方法。
private File file; ——上传的文件,变量名对应页面上"file"input的name属性值。类型为java.io.File
private String fileContentType;——上传文件的格式类型名,变量名格式为:页面上"file"input的name属性值+ContentType
private String fileFileName——上传的文件名,变量名格式为:页面上"file"input的name属性值+fileFileName。
2. 使用struts2提供的FileUtils类拷贝进行文件的拷贝。FileUtils类位于org.apache.commons.io包下。
3. 在项目目录下的WebContent目录下添加 upload 文件夹,用于存放客户端上传过来的文件,对应第15行代码。
Upload.java代码如下:

1 import java.io.File;
2 import java.io.IOException;
3 import org.apache.commons.io.FileUtils;
4 import org.apache.struts2.ServletActionContext;
5 import com.opensymphony.xwork2.ActionSupport;
6
7 public class Upload extends ActionSupport{
8 private File file;
9 private String fileContentType;
10 private String fileFileName;
11
12 @Override
13 public String execute() throws Exception {
14 //得到上传文件在服务器的路径加文件名
15 String target=ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName);
16 //获得上传的文件
17 File targetFile=new File(target);
18 //通过struts2提供的FileUtils类拷贝
19 try {
20 FileUtils.File(file, targetFile);
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24 return SUCCESS;
25 }
26
27 //省略get,set方法...........
28
29 }

三、在struts.xml中添加相应的配置代码。
struts.xml代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="Upload">
<result>index.jsp</result>
</action>
</package>
</struts>

四、测试。
启动服务器,进入index页面。

选择一改图片,点击上传提交表单。

打开upload文件夹(注意,这里指的是web服务器下的目录,如我用的web服务器是tomcat安装在电脑D盘,项目名称为“Struts2Upload”那么其路径为:D:\apache-tomcat-7.0.40\webapps\Struts2Upload\upload)可以看到刚才选中的图片已经上传到该目录下了。

上传多个文件
一、修改页面文件
增加继续添加按钮和 addfile() 方法,让页面可以通过javascript增加 input 标签。
修改后的 index.jsp代码如下:

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <script type="text/javascript">
8 //添加javascript方法 addfile() 在页面中境加input标签、
9 function addfile(){
10 var file = document.createElement("input");
11 file.type="file";
12 file.name="file";
13 document.getElementById("fileList").appendChild(file);
14 document.getElementById("fileList").appendChild(document.createElement("br"));
15 }
16 </script>
17 <title>Insert title here</title>
18 </head>
19 <body>
20 <!-- 注意!表单必须添加 enctype 属性,值为"multipart/form-data" -->
21 <form action="upload.action" method="post" enctype="multipart/form-data">
22 <div id="fileList">
23 <input type="file" name="file" /><br/>
24 </div>
25 <!-- 添加继续添加按钮,点击按钮调用addfile() -->
26 <input type="button" value="继续添加" onclick="addfile()" />
27 <input type="submit" value="上传"/>
28 </form>
29 </body>
30 </html>

二、修改Action类
1. 把三个私有字段(file,fileContentType,fileFileName)的类型更改为数组或集合类型。并添加相应的get,set方法。
2. 通过循环来上传多个文件。
修改后的Upload.java代码如下:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Upload extends ActionSupport{
//把这三个字段类型更改为数组或集合
private File[] file;
private String[] fileContentType;
private String[] fileFileName;

@Override
public String execute() throws Exception {
//通过循环来上传多个文件
for(int i=0;i<file.length;i++){
//得到上传文件在服务器的路径加文件名
String target=ServletActionContext.getServletContext().getRealPath("/upload/"+fileFileName[i]);
//获得上传的文件
File targetFile=new File(target);
//通过struts2提供的FileUtils类拷贝
try {
FileUtils.File(file[i], targetFile);
} catch (IOException e) {
e.printStackTrace();
}
}
return SUCCESS;

}

//省略set,get方法...................

}

三、测试
1. 启动服务器,打开index.jsp页面。点击继续添加,添加两个input。同时上传三张图片。

2. 点击上传提交表单。打开upload文件夹,可以看到刚才选中的三张图片已经上传到该目录下了。

参考资料http://www.cnblogs.com/likailan/p/3330465.html

G. struts2 上传多个文件的问题

获得对应上传框中的文件数组,文件名数组和文件类型数组这个容易,你看下面代码就知道了import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.Action;public class uploadfile implements Action,ServletRequestAware {

HttpServletRequest request;
private File[] file;
private String[] fileContentType;
private String[] fileFileName;
HttpServletRequest request;
HttpSession session; public void setServletRequest(HttpServletRequest request)
{this.request=request;}
public File[] getFile() {
return file;
}
public void setFile(File[] file) {
this.file = file;
}
public String[] getFileContentType() {
return fileContentType;
}
public void setFileContentType(String[] fileContentType) {
this.fileContentType = fileContentType;
}
public String[] getFileFileName() {
return fileFileName;
}
public void setFileFileName(String[] fileFileName) {
this.fileFileName = fileFileName;
}*/ public String execute() throws Exception
{

session=request.getSession();
ConBean conbean=new ConBean();
conbean.con(); //连接数据库

String name=(String)session.getAttribute("name");

for(int i=0;i<file.length;i++)
{
String address="resource/"+getFileFileName()[i];
String time=new Date().toString();
String sql="insert into resource(name,s_name,s_address,s_time) values('"+name+"','"+getFileFileName()[i]+"','"+address+"','"+time+"')";
conbean.insert(sql);

FileOutputStream w=new FileOutputStream("../webapps/webDemo/resource/"+getFileFileName()[i]);
FileInputStream r= new FileInputStream(getFile()[i]);

byte[] bt=new byte[19999999];

int len=0;
if((len=r.read(bt))>0)
{
w.write(bt, 0, len);
}

w.close();
r.close();

}

return SUCCESS;}

H. struts2 上传多个文件的问题

发你的邮箱来,我发一个文件给你看看吧,是我以前做的

I. 如何用struts上传多个多个文件

用struts上传多个多个文件,下面从两方面说:

1、Action

J. 使用struts2如何实现文件上传

  1. 新建Web Project,在WebRoot下新建upload文件夹

  2. 在WebRoot下新建upload.jsp,上传界面

  3. 编写上传成功、失败的提示界面。

  4. 在WebRoot下新建uploadError.jsp

  5. 在WebRoot下新建uploadSuccess.jsp

  6. 编写Action类

  7. 配置struts.xml文件,重置fileUpload拦截器。

  8. 测试,测试完成之后在tomcat下面webapps目录下找到项目对应的文件夹下的upload下查看

热点内容
如何编写sql的lua脚本 发布:2025-08-27 05:50:30 浏览:915
ultraedit反编译 发布:2025-08-27 05:48:15 浏览:836
电源编程 发布:2025-08-27 05:42:18 浏览:623
mysql存储过程elseif 发布:2025-08-27 05:20:02 浏览:146
python的内存机制 发布:2025-08-27 05:19:29 浏览:424
如何反编译通达信 发布:2025-08-27 05:02:01 浏览:701
网吧停电后怎么重启服务器 发布:2025-08-27 05:01:20 浏览:953
宣传片创意文案和脚本 发布:2025-08-27 04:48:41 浏览:27
c语言产生不同随机数 发布:2025-08-27 04:12:32 浏览:177
phpbaidu 发布:2025-08-27 03:31:32 浏览:673