导航:首页 > 版本升级 > struts2文件上传例子

struts2文件上传例子

发布时间:2024-12-28 09:33:58

A. 使用struts2 怎么实现 文件上传到另外一台电脑

传输文件的功能,类似于网络聊天程序

肯定要用的文件传输用的IO流,还有网络专通信用到属的socket了。

可以在你部署struts2网站的服务器上面写一个网络通信程序(服务器端),对应的网络通信程序(客户端)放在“另外一台电脑”上面。

网站的服务器启动之后:
1。把那个网络通信程序(服务器端)启动

2。把“另外一台电脑”上面的网络通信程序(客户端)启动,现在两端就建立连接了。

3。可以通过服务器端向客户端发送数据。

以上过程跟我们平时用的聊天程序一样。

你可以在网上看看相应的网络聊天程序,现在网上这样的程序还是很多的。
里面实现了这样的机制。

B. 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>

C. 请问struts2能实现上百兆的视频的上传和下载吗如果可以,怎么配置文件类型

<!--上传文件的大小限制-->
<constant name = "struts.multipart.maxSize" value = "10701096" />

在struts.xml中加如上面的代码,其中的value值指的是文件的大小,以B为单位的.
<struts>
<constant name = "struts.multipart.maxSize" value = "10701096" />

<package name="test1" namespace="test1" extends="struts-default">
<action name="massage_*" class="org.zl.action.HelloWorldAction">
<result name="success">/WEB-INF/page/massage.jsp</result>
</action>
</package>

</struts>

D. commons-httpclient如何实现文件上传

在struts2中结合HttpClient进行文件上传
最近遇到了用httpclient进行上传文件的问题,下面我就和大家简单的说一下:
package com.imps.action;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class TabinfoAction extends BaseAction
{
private File[] myFile;
private String[] myFileFileName;// 文件名
private Integer[] myFileFileSize;// 文件大小
private String[] myFileContentType;// 文件类型

public String uploadPost()
{
String posturl ="http://127.0.0.1:8080/settabimage.aspx";
System.out.println(posturl);
String status=null;
for(int i=0;i<myFile.length;i++)
{
FileInputStream file=new FileInputStream(myFile[i]);
InputStream in = new BufferedInputStream(file);
PostMethod post=new PostMethod(posturl);
post.setRequestBody(in);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response=new String(post.getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8");
post.releaseConnection();
in.close();
file.close();
}
}

public File[] getMyFile() {
return myFile;
}

public void setMyFile(File[] myFile) {
this.myFile = myFile;
}

public String[] getMyFileFileName() {
return myFileFileName;
}

public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}

public Integer[] getMyFileFileSize() {
return myFileFileSize;
}

public void setMyFileFileSize(Integer[] myFileFileSize) {
this.myFileFileSize = myFileFileSize;
}

public String[] getMyFileContentType() {
return myFileContentType;
}

public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}
千万记住不要记忘记关闭流和释放http连接

E. struts2中的s:file标签怎么限定上传文件为图片类型,最好是能在选择文件时就只能选择图片文件

<struts>
<package name="default" extends="struts-default">
<action name="upload" class="g2w.struts2.FileUploadAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
text/plain,text/css,text/javascript
</param>
<param name="maximumSize">1000000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<param name="savePath">/uploads</param>
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
<result name="input">/failure.jsp</result>
</action>
</package>
</struts>

package g2w.struts2;

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 FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private File file;
private String fileContentType;
private String fileFileName;

private String savePath;

// setters & getters

public String execute() {
try {
File destFile = new File(this.getSavePath(), fileFileName);
FileUtils.File(this.file, destFile);
// ....
} catch (IOException e) {
this.setMessage(e.getMessage());
return ActionSupport.ERROR;
}
return SUCCESS;
}
}

F. struts2文件上传中,如何限制上传的文件类型

只需要在struts配置文件中配置就OK了
案例如下:

<package name="upload" extends="struts-default" namespace="/upload">
<!-- 配置 -->
<action name="upload" class="www.ijava.com.UploadAction" >

<param name="savePath">e:/images/</param>

<!--往fileuploadInterceptor 注入 -->
<interceptor-ref name="defaultStack">
<!-- 改变当前文件运行上传的类型 -->
<param name="fileUpload.allowedTypes">image/jpeg,image/jpg</param>
<!-- 允许的文件后缀 -->
<param name="fileUpload.allowedExtensions">jpg,jpeg,gif</param>
</interceptor-ref>
<result>/index.jsp</result>
</action>

G. 关于struts2框架的文件上传问题。。。上传的文件超过2MB就报下面的异常,请问怎么解决

在struts.xml中设置
<constant name="struts.multipart.maxSize" value="314572800"></constant> <!-- 允许300M -->
可以允许上传300M的呢!我试了下,上传了个202M的电版影,竟然上传成功了!权

H. 我用struts2做的上传文件功能,但当文件超过2G时页面出现错误,这个有什么办法解决么

struts配置文件里有来个上传大自小的常量可以配
可以配置default.properties文件,也可以直接配xml常量
常量名是struts.multipart.maxSize
值的话你自己换算就好了 1K*1024*1024什么什么的自己乘完放到value里

阅读全文

与struts2文件上传例子相关的资料

热点内容
网易云音乐2016数据库 浏览:532
武器用什么编程语言 浏览:617
下载的文件在电脑桌面找不到 浏览:896
iphone锁屏播放视频插件 浏览:372
尿液体检中哪个数据表示怀孕 浏览:575
苹果使用lcloud的app是什么意思 浏览:924
网商银行app790游戏大厅 浏览:487
网站授权访问打不开怎么回事 浏览:6
h5网站定制需要多少钱 浏览:933
伦敦金正规平台有哪些app 浏览:824
有什么网站是一级域名 浏览:602
protel中找不到pcb文件 浏览:705
什么编程语言最少 浏览:557
微信时代的爱 浏览:245
iphone5s香港直营店 浏览:690
js验证码判断 浏览:394
苹果5c屏幕显示对比 浏览:586
win7应用程序打不开 浏览:914
foxmail收qq邮件密码错误 浏览:716
网站源码超市下载 浏览:641

友情链接