前台页面jsp

<!--

拦截的时候用这个

<s:form action="uploadAction" enctype="multipart/form-data" method="post">
<label>上传文件:</label><br/>
<s:file name="myfiles"></s:file> <br/>
<s:file name="myfiles"></s:file> <br/>
<s:file name="myfiles"></s:file> <br/>
<s:file name="myfiles"></s:file> <br/>
<s:submit value="提交"></s:submit>
</s:form>

-->

<form action="uploadAction" enctype="multipart/form-data" method="post">
<label>上传文件:</label>
<input type="file" name="myfile"/>
<input type="submit" value="提交"/>
</form>

action

public class UploadAction extends ActionSupport {
//三个全局属性注意命名规则,属性名的前半部分保持一致,不然报空值
//上传的文件(旧文件)
private File myfile;
//上传的文件名(旧文件)
private String myfileFileName;
//上传文件类型(旧文件)
private String myfileFileContentType;

//处理上传请求
public String upload(){
//生成新的文件名(使用uuid)
String newmyfilename = UUIDUtil.getUUID()+myfileFileName.substring(myfileFileName.lastIndexOf("."));
//指定上传的位置
String path = ServletActionContext.getServletContext().getRealPath("upload");
//上传文件的位置
String filepath = path+File.pathSeparator+newmyfilename;
System.out.println("filepath = "+filepath);
//构建新文件
File newfile = new File(filepath);

//读入写出 从旧文件读内容到新文件
FileInputStream fis = null;
FileOutputStream fos = null;

try {
//将旧文件封装到输入流
fis = new FileInputStream(myfile);
//将新文件封装到输出流
fos = new FileOutputStream(newfile);
//设置一个字节数组缓冲内容
byte [] bt = new byte[1024];
int len = 0;
/**
* 循环读取缓冲区的内容
* 输入流不断的将字节读入到缓冲区(旧文件到缓冲区)
* 输出流不断的将字节写出到新文件(缓冲区到新文件)
*/
while((len = fis.read(bt))!=-1){
fos.write(bt, 0, len);
}
fos.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return SUCCESS;
}

public File getMyfile() {
return myfile;
}

public void setMyfile(File myfile) {
this.myfile = myfile;
}

public String getMyfileFileContentType() {
return myfileFileContentType;
}

public void setMyfileFileContentType(String myfileFileContentType) {
this.myfileFileContentType = myfileFileContentType;
}

public String getMyfileFileName() {
return myfileFileName;
}

public void setMyfileFileName(String myfileFileName) {
this.myfileFileName = myfileFileName;
}

}

struts.xml

<!-- struts2中文件上传拦截
struts2 的核心包下的default.properties文件里有默认的大小为struts.multipart.maxSize=2097152,也就是2M. 这是struts2默认拦截,
解决方法:在struts.xml配置文件中,添加
<constant name="struts.multipart.maxSize" value="10485760"/>
这里的value单位为B,即10485760B = 10MB。
-->
<constant name="struts.multipart.maxSize" value="10485760"/>

<!--读取国际化文件

该常量用于读取国际化文件
  name表示国际化资源
  value表示国际化文件所在位置,
注意:国际化文件不要写后缀

-->

<constant name="struts.custom.i18n.resources" value="com.oak.action.myUpload"></constant>
<package name="upload" namespace="/" extends="struts-default">
<action name="uploadAction" class="com.oak.action.UploadAction" method="upload">

<!-- fileUpload拦截器是系统拦截器,只需要引用,单词是固定的 -->
<interceptor-ref name="fileUpload">
<!-- 允许用户上传文件的大小,单位是字节 10M -->
<param name="maximumSize">10485760</param>
<!-- 允许用户上传文件的扩展名,如果不设置,则不受限,多个可以以逗号分隔 -->
<param name="allowedExtensions">.jpg,.txt,.jsp</param>
</interceptor-ref>
<!--还需要引用系统默认的拦截器-->
<interceptor-ref name="validationWorkflowStack"></interceptor-ref>
<interceptor-ref name="basicStack"></interceptor-ref>

<result>
/welcome.jsp
</result>

<!-- 用于输出错误信息到页面 -->
<result name="input">
/upload.jsp
</result>

</action>
</package>

struts2之单文件上传(7)的更多相关文章

  1. Struts2实现单文件上传

    首先配置一下web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ...

  2. Struts2单文件上传原理及示例

    一.文件上传的原理 表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值: 1.application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里 ...

  3. 【Struts2学习笔记(9)】单文件上传和多文件上传

    (1)单文件上传 第一步:在WEB-INF/lib下增加commons-fileupload-1.2.1.jar.commons-io-1.3.2.jar. 这两个文件能够从http://common ...

  4. Struts2 单文件上传

    Struts2 提供了更为简便的文件上传机制,将文件上传的复杂操作都封装到commons-fileupload.jar .commons-io.jar两个jar包中,然后再程序中使用简单的几句代码就能 ...

  5. Struts2 之 实现文件上传和下载

    Struts2  之 实现文件上传和下载 必须要引入的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar 01.文件上传需要分别在struts.xm ...

  6. ajaxFileUpload+struts2实现多文件上传

    以前有介绍过ajaxFileUpload实现文件上传,但那是单文件的,这次介绍多文件上传. 单文件上传参考:http://blog.csdn.net/itmyhome1990/article/deta ...

  7. 关于Struts2的多文件上传

    之前写过一篇文章,关于Struts2文件上传:http://www.cnblogs.com/lichenwei/p/3927964.html 现在来说下多文件上传,其实就把上传文件当成是一个数组去处理 ...

  8. [转]Struts2多个文件上传

    转载至:http://blog.csdn.net/hanxiaoshuang123/article/details/7342091 Struts2多个文件上传多个文件上传分为List集合和数组,下面我 ...

  9. 笨鸟先飞之Java(一)--使用struts2框架实现文件上传

    无论是.net还是Java,我们最常接触到的就是文件的上传和下载功能,在Java里要实现这两个经常使用功能会有非常多种解决方案,可是struts2的框架却能给我们一个比較简单的方式,以下就一起来看吧: ...

随机推荐

  1. [转帖]被HTTP/2漏洞拖累,所有Kubernetes版本受影响

    被HTTP/2漏洞拖累,所有Kubernetes版本受影响 https://www.kubernetes.org.cn/5746.html 服务很重要啊... 低版本都不解决安全问题了.. 不过HTT ...

  2. 2019年9月(System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本。)问题解决记录

    System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本. 在百度上寻找了很久,都说是权限的问题,可是更改过后一点效果也没有. 实际上真 ...

  3. oracle共享数据库操作

    Hello,大家好,这个功能相信新手小白很需要,今天小编因为刚好遇到,所以写出来分享给大家,首先你电脑得有数据库,以及PLSQL工具包,这个相信大家都有了 1.打开NET Manger应用,win10 ...

  4. SQLite进阶-15.触发器

    目录 触发器(Trigger) 触发器(Trigger)的要点: 触发器应用 查看触发器 删除触发器 触发器(Trigger) 触发器(Trigger)是数据库的回调函数,它会在指定的数据库事件发生时 ...

  5. 用JavaScript写一个简单的倒计时,可以应用在发送短信验证码的“59秒后重新发送验证短信”

    倒计时——从10倒数到0,点击按钮会还原倒计时 <body> <!-- 将textvalue值设为10,从10倒数 --> <input type="text& ...

  6. 基于DNS(Consul)高可用

    DNS 推荐从Bind-DLZ入手,资料多可控制度更好(查询DNS记录SQL可定制)据说性能差 Bind-DLZhttps://www.cnblogs.com/saneri/p/8178065.htm ...

  7. Sql语句知识大全

    1.经典SQL语句大全(绝对的经典) 2. 3. 4.一.基础 1.1.说明:创建数据库 2.CREATE DATABASE database-name 3.2.说明:删除数据库 4.drop dat ...

  8. spring利用xml配置定时任务

    在开发中会经常遇到做定时任务的需求,例如日志定时清理与处理,数据信息定时同步等需求. 1.在spring中利用xml配置定时任务,如下 <!-- ftpiptv信息同步接口定时任务配置--> ...

  9. Apache httpclient的execute方法调试

    因为工作需要,想研究一下execute执行的逻辑. 在这一行调用execute: response = getHttpClient().execute(get); getHttpClient的实现: ...

  10. 了解jQuery的detach()和remove()

    jQuery中提供了两种移出一个DOM元素的方法detach()和remove(),虽然是一样的功能,但是给出两种方法,必然有它的不同之处. empty() 单独说一下 ,它删除当前元素的所有子元素, ...