struts2上传配置
‘壹’ 如何配置struts2
这么简单!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 设置字符集 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!--项目开发struts.devMode=true -->
<constant name="struts.devMode" value="true" />
<!-- 自动加载国际化资源文件,开发建议为ture -->
<constant name="struts.i18n.reload" value="true" />
<!--自动加载struts配置文件,开发建议为true -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置游览器是否使用浏览器缓存,开发建议为false -->
<constant name="struts.serve.static.browserCache" value="false" />
<constant name="struts.ui.theme" value="simple" />
<!-- 添加struts配置文件 -->
<include file="config/struts/struts_book.xml" />
<include file="config/struts/struts_cart.xml" />
<include file="config/struts/struts_order.xml" />
<include file="config/struts/struts_user.xml" />
<package name="dang-default" extends="struts-default"
namespace="/base">
<!-- 公共struts信息 -->
<!-- 定义事务拦截器 -->
<interceptors>
<interceptor name="transactionInterceptor"
class="com.tarena.web.interceptor.TransactionInterceptor" />
<interceptor-stack name="dangStack">
<interceptor-ref name="transactionInterceptor" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 定义默认拦截器 -->
<default-interceptor-ref name="dangStack" />
<global-results>
<result name="login" type="redirect">/user/login_form.jsp</result>
</global-results>
</package>
</struts>
‘贰’ struts2文件上传有没有最大限制
Struts2上传文件的默认大小限制是2M(2097152字节),也可以通过struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上传文件的总大小不能超过2K字节。
struts2上传非常方便了,没听说网上有很多上传插件,上传核心代码在后台,看你怎么获取上传的文件数据怎么将数据再写入到本地服务器。
‘叁’ struts2 文件上传怎么指定保存文件的路径和大小
在action配置文件struts.xml里设置(如下):
<package name="upload" extends="struts-default">
<action name="upload" class="">
<!--配置fieldUpload拦截器--->
<interceptor-ref name="fileUpoad">
<param name="allowedTypes">image/bmp,image/png,image/jpg,image/gif</param>
<param name="maximumSize">200000</param>
</interceptor-ref>
<!---必须显示配置引用struts默认的拦截器栈:defaultStack----->
<interceptor name="defaultStack"></interceptor>
<!---设置上传路径----->
<param name="savePath">/upload</param>
<result name="success">/upload_succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
希望能帮到你哈....
‘肆’ struts2文件上传和下载
1,上传方法
(1),页面form表单添加一个属性为enctype="multipart/form-data" 和method="post"
(2),假设上传预览框为 <input type="file" name="myfile" />
(3),控制器接值的方法为
private File myfile; //要上传的文件
private String myfileFileName; //要上传文件名称
private String myfileContentType; //要上传文件类型
别忘了做set方法
(4), 接到值后可以保存到数据库,也可以保存到硬盘,
>>1 保存到数据库, 数据库表中对应字段要设置为BLOB类型
>>2 保存到硬盘代码如下
InputStream in = new
FileInputStream( myfile);
OutputStream out = new
FileOutputStream( new File("d:\\upload\\"+myfileFileName));
byte[] buffer
= new byte[ in.available() ];
int ins =
in.read(buffer);//读取字节到buffer中
//ins == -1 时
。就已经是文件的结尾了
while ( ins !=
-1 ) {
out.write(buffer, 0, ins);//将缓存buffer中的数据写到文件中
ins = in.read(buffer);
}
in.close();
out.flush();
out.close();
2,下载
(1), 把要下载的文件转成一个输入流InputStream
例如,利用hibernate取得一个文件,文件类型在实体类中为byte[]类型,
inputStream = new
ByteArrayInputStream(book.getMyfile);
其中inputStream 为全局变量,并且做setter和getter方法
(2),在控制器对应的action节点中(struts2配置文件中)添加一个result节点如下:
<result name="download" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${myFileFileName}"</param>
<param name="bufferSize">1024</param>
</result>
这样,就可以实现上传和下载了.
‘伍’ 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>
‘陆’ 使用struts2 怎么实现 文件上传到另外一台电脑
传输文件的功能,类似于网络聊天程序。
肯定要用的文件传输用的IO流,还有网络通信用到的socket了。
可以在你部署struts2网站的服务器上面写一个网络通信程序(服务器端),对应的网络通信程序(客户端)放在“另外一台电脑”上面。
网站的服务器启动之后:
1。把那个网络通信程序(服务器端)启动
2。把“另外一台电脑”上面的网络通信程序(客户端)启动,现在两端就建立连接了。
3。可以通过服务器端向客户端发送数据。
以上过程跟我们平时用的聊天程序一样。
你可以在网上看看相应的网络聊天程序,现在网上这样的程序还是很多的。
里面实现了这样的机制。