struts2上传多个文件
A. struts2上传多个文件时struts.xml中怎么写
上传下载都有。。。仅仅作为参考。有问题可以追问。
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<constant name="struts.custom.i18n.Encoding" value="GBK" />
<constant name="strurs.devMode" value="true" />
<constant name="struts.multipart.saveDir" value="e:/temp" />
- <package name="default" extends="struts-default">
- <action name="upload" class="com.bbs.upload.action.UploadAction">
<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
<param name="savePath">/upload</param>
<result name="input">/error.jsp</result>
<result name="success">/success.jsp</result>
</action>
- <action name="download" class="com.bbs.upload.action.DownloadAction">
<param name="fileName" />
- <result name="success" type="stream">
- <!-- <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg,image/jpg</param>
-->
- <!-- <param name="contentType">text/plain</param>
-->
<param name="inputName=">targetFile</param>
<param name="contentDisposition">fileName="bg.png"</param>
<param name="inputName">downloadFile</param>
</result>
</action>
- <action name="*">
<result>/{1}.jsp</result>
</action>
</package>
</struts>
B. struts2 如何实现上传整个文件夹的功能
一、压缩文件其实是可以0压缩率直接打包,这样其实蛮快的
二、看到网上说Applet可以上传文件夹,具体远离不清楚,你可以看看
三、最笨的方法,用Ajax做一个递归遍历文件夹的函数,如果是文件就上传上去,如果是文件夹就请求后台新建文件夹
四、用JSON格式把目录和文件封装起来,统一传到后台,但是后台处理要比较麻烦
C. struts2批量上传文件提示以下异常
struts.multipart.saveDir没有配置。
struts.multipart.saveDir用于指定存放临时文件的文件夹,
于是在,struts.xml配置文件中增加如下:
<constant
name="struts.multipart.saveDir"
value="/tmp"/>
设置临时文件上传路径。
D. 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";
}
}
E. Struts2上传多个文件并写入数据库
一般情况下,完成上传文件后把文件的路径保存到数据库中就可以了,o(∩_∩)o~
F. struts2可以上传整个文件夹(里面全是图片)吗求指点
可以 ,只要你将文件类型制定好就可以了,但是你必须想将其压缩
例如<param name="allowedTypes">file/jar,file/zip</param>
G. Struts2 多文件上传,高手请进!!
首先,你要知道怎么在action是区分文件。struts2可以同时上传多文件,action接收到的是一个File list,如果你要区分只能从文件的contentType进行差别,但不同浏览器对同一种文件类型的contentType有可能不同。所以,你要知道不同浏览器对文件的contentType。
最后,就是在action里得到得到文件的contentType进行判断,对不同的文件进行不同处理就行了。
当然你也可以用文件的后缀进行判断,不过不推荐使用后缀进行文件类型的判断
H. 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>
I. struts2如何同时上传文件以及获得表单数据
事实上这根本不需要什么其他配置操作,因为这是Struts2,而不是原生Servlet,在Struts2中,拦截器会将request中的表单数据(或者文件格式的数据)都和action类中的属性名称一一对应的注入值(包括文件数据)。所以你需要做的,其实只是在jsp页面(或html)中加入一个file类型的input标签,名称记住(比如为photo),然后在action类中加一个File类型(java.io.File)字段,此字段必须和刚刚的input标签name属性一致,即photo(private File photo;)。最后,需要注意的是,当你妄图从网页上传一个文件类型的表单时,必须将包围它的form类将enctype="multipart/form-data" method="post"加上,即method必须为post,且enctype,也就是表单数据类型,必须为二进制的。
J. struts2批量上传的问题
1、import org.omg.CORBA.Request; 这个类不是Servlet的Request
2、没看到你对javax.servlet.ServletContext的导入,语法上一定会报错的