单文件上传

上传页面

<%@ 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>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="upload"><br><br>
上传者:<input type="text" name="author">
<input type="submit" value="上传">
</form>
</body>
</html>

struts.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="jiangwenwen" namespace="/" extends="struts-default">
<action name="upload" class="cn.jiangwenwen.action.UploadAction" method="fileUpload">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>

Action类

public class UploadAction  extends ActionSupport{

	//存放上传的文件对象
private File upload;
//上传的文件名称
private String uploadFileName;
//上传的上传者
private String author; public String fileUpload() throws IOException { FileInputStream fis = new FileInputStream(upload); String path = "D://pic/"+uploadFileName; FileOutputStream fos = new FileOutputStream(path); int flag = 0; while((flag=fis.read())!=-1) {
fos.write(flag);
}
fis.close();
fos.close(); return SUCCESS; } 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 getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} }

成功接收页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<!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>
<!-- 此 /pic为服务器配置的路径-->
<img src="/pic/${uploadFileName }">
</body>
</html>

批量上传

上传页面

<%@ 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>
<form action="upload.action" method="post" enctype="multipart/form-data">
文件:<input type="file" name="upload"><br><br>
<input type="file" name="upload"><br><br> 上传者:<input type="text" name="author">
<input type="submit" value="上传">
</form>
</body>
</html>

struts.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 修改文件上传大小限制 -->
<constant name="struts.multipart.maxSize" value="11111111111111111"></constant>
<!-- 开启国际化,value的值是配置文件的名称(在src目录下)-->
<constant name="struts.custom.i18n.resources" value="message" />
<!-- 开启开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
<package name="jiangwenwen" namespace="/" extends="struts-default">
<action name="upload" class="cn.jiangwenwen.action.UploadAction" method="upload">
<result name="success">/success.jsp</result>
<result name="input">/error.jsp</result>
<!-- 控制单个文件上传大小 -->
<interceptor-ref name="fileUpload">
<param name="maximumSize">
1500
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>

Action类

public class UploadAction  extends ActionSupport{
//上传的文件对象
private File[] upload;
//上传的文件名称
private String[] uploadFileName;
//上传的文件类型
private String[] uploadContentType; public String upload() { String path = "D://pic/"; for(int i=0;i<upload.length;i++) {
upload[i].renameTo(new File(path,uploadFileName[i]));
System.out.println("上传成功");
} return SUCCESS;
} 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[] getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
}

成功接收页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>
<s:debug></s:debug>
</body>
</html>

Struts2之上传的更多相关文章

  1. FTP文件操作之上传文件

    上传文件是一个比较常用的功能,前段时间就做了一个上传图片的模块.开始采用的是共享文件夹的方式,后来发现这种方法不太好.于是果断将其毙掉,后来选择采用FTP的方式进行上传.个人感觉FTP的方式还是比较好 ...

  2. Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法

    Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法 在使用struts2的项目中上传文件的时候出现了一个这样的错误: 2011-7 ...

  3. [JavaWeb基础] 009.Struts2 上传文件

    在web开发中,我们经常遇到要把文件上传下载的功能,这篇文章旨在指导大家完成文件上传功能 1.首先我们需要一个上传文件的页面. <!--在进行文件上传时,表单提交方式一定要是post的方式, 因 ...

  4. 利用struts2上传文件时,如果文件名中含有-符号,那么会出错

    利用struts2上传文件时,如果文件名中含有-符号,那么会出错 报错如下: HTTP Status 500 - C:\Program Files\Apache Software Foundation ...

  5. struts2上传的问题

    5. 在这里我加一个struts2的一个上传验证的问题 上传时我们可以这样来验证 //判断上传的文件是否合要求 public boolean filterType(String []types){ / ...

  6. struts2上传文件添加进度条

    给文件上传添加进度条,整了两天终于成功了. 想要添加一个上传的进度条,通过分析,应该是需要不断的去访问服务器,询问上传文件的大小.通过已上传文件的大小, 和上传文件的总长度来评估上传的进度. 实现监听 ...

  7. 关于Struts2上传文件的最大Size的设置

    今天使用Struts2的文件上传控件时,在struts.xml中,将处理上传的action中的fileUpload拦截器的maximumSize参数设置为5000000,上传了一个3M的文件后发现控制 ...

  8. struts2上传下载

    struts上传下载必须引入两个jar文件: commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件 import java.io.BufferedI ...

  9. 菜鸟学SSH(五)——Struts2上传文件

    上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能.今天我们就一起来学习一下Struts2如何上传文件. 今天讲的上传文件的方式有三种: 1,以字节为单位传输文件: 2,Struts2封 ...

随机推荐

  1. h5与app交互

    现在移动端 web 应用,很多时候都需要与原生 app 进行交互.沟通(运行在 webview中),比如微信的 jssdk,通过 window.wx 对象调用一些原生 app 的功能.所以,这次就来捋 ...

  2. Spark2.0集成Hive操作的相关配置与注意事项

    前言 已完成安装Apache Hive,具体安装步骤请参照,Linux基于Hadoop2.8.0集群安装配置Hive2.1.1及基础操作 补充说明 Hive中metastore(元数据存储)的三种方式 ...

  3. Uedit32_17.00 修改某一语言背景色-修改后续名后语法着色及某语言的大括号{}对齐

    修改UE的背景色:高级-配置-编辑器显示-其它-设置颜色 新增扩展名语法着色:如以tpl为后缀的html代码格式着色高级-配置-编辑器显示-语法着色-语言选言[选中要着色的语言html]-打开-在'F ...

  4. MT41J256M16HA-125 原厂订购 现货销售

    作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...

  5. OGG-00664

    参数SID写错了 GGSCI (t2) > edit param exta extract exta setenv (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK) se ...

  6. openGL常用对象的创建及使用

    一.GPU英文全称Graphic Processing Unit,中文翻译为“图形处理器”.GPU(显卡核心芯片)是显示卡的“大脑”,它决定了该显卡的档次和大部分性能 二.使用背景 随着OpenGL状 ...

  7. bzoj5118 Fib数列2 二次剩余+矩阵快速幂

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5118 题解 这个题一看就是不可做的样子. 求斐波那契数列的第 \(n\) 项,\(n \leq ...

  8. 路由Vue-router 的使用总结

    1.关于 router-view 匹配 vue 项目使用 vue-router,所有的根级别的路由都是在 App.vue 文件中的 router-view 中渲染的.比如下面的 path: '/' . ...

  9. asp.net大文件断点续传

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

  10. Leetcode_395. Longest Substring with At Least K Repeating Characters_[Devide and Conquer]

    题目链接 对一个字符串,找出一个最长的子串长度,这个子串中所有字符出现的次数至少为k. 1.滑动窗口 一开始把题目看成了,子串中每个字符至多出现k次.如果是这样,那么是一道典型的滑动窗口的题目. 然而 ...