云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > java struts2入门学习实例--使用struts2快速实现多个文件上传

java struts2入门学习实例--使用struts2快速实现多个文件上传

  • 62 次阅读
  • 3 次下载
  • 2025/12/23 20:08:17

一、错误提示信息配置

昨天说到更改默认错误配置信息,我测试很多遍,一直都不对。下面贴出来,待以后有好方法了再补充吧。

首先新建一个properties文件,这里命名为testupload.properties,内容为:

struts.messages.error.file.too.large=\文\件\太\大{0} \\{3}

struts.messages.error.content.type.not.allowed=\文\件\类\型\不\允\许! {0} \\{3}

struts.messages.error.file.extension.not.allowed=\不\允\许\的\扩\展\名! {0} \\{3}

这里将默认提示信息改为中文的。upload.xml中内容如下:

\Software Foundation//DTD Struts Configuration 2.3//EN\ \>

二、多个文件上传

只需要更改upload.jsp中的部分内容即可:

<%@ page language=\ contentType=\charset=UTF-8\ pageEncoding=\%>

<%@ taglib uri=\ prefix=\ %>

www.qqok8.com

Insert title here

结果如下所示:

也可以将UploadAction.java中的file改为数组类型的,如下所示:

package action;

import java.io.File;

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; http://mingzi.78name.com

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport; /**

* @ClassName: UploadAction * @Description: 文件上传 * @author: amosli

* @email:amosli@infomorrow.com * @date Jan 14, 2014 1:50:28 AM */

public class UploadAction extends ActionSupport {

private static final long serialVersionUID = -8920466592471253212L; private String username;// 用户名

private String[] uploadContentType;// 上传文件的类型,(Fileupload拦截器传入的参数)

private File[] upload;// 上传的文件,(Fileupload拦截器传入的参数)

private String[] uploadFileName;// 上传文件的真实文件名,(Fileupload拦截器传入的参数)

public void setUsername(String username) { this.username = username; }

public String[] getUploadContentType() { return uploadContentType; }

public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; }

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 String getUsername() { return username; }

public String uploadMethod() throws Exception {

ServletContext context = ServletActionContext.getServletContext(); String real_path = context.getRealPath(\); for (int i = 0; i < upload.length; i++) {

InputStream inputStream = new FileInputStream(upload[i]);

OutputStream outputStream = new FileOutputStream(real_path + \/\+ uploadFileName);

byte[] b = new byte[1024]; int len = 0;

搜索更多关于: java struts2入门学习实例--使用struts2快 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

一、错误提示信息配置 昨天说到更改默认错误配置信息,我测试很多遍,一直都不对。下面贴出来,待以后有好方法了再补充吧。 首先新建一个properties文件,这里命名为testupload.properties,内容为: struts.messages.error.file.too.large=\文\件\太\大{0} \\{3} struts.messages.error.content.type.not.allowed=\文\件\类\型\不\允\许! {0} \\{3} struts.messages.error.file.extension.not.allowed=\不\允\许\的\扩\展\名! {0} \\{3} 这里将默认提示信息改为中文的。upload.xml中内容如下:

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com