前台页面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. python的u,r,b分别什么意思?

      我们经常在python当中看到以下内容: print(u'hi\thi\thi') print(b'hi\thi\thi') print(r'hi\thi\thi') 在其他语言里没见过类似的,所 ...

  2. 学习笔记:CentOS7学习之二十二: 结构化命令case和for、while循环

    目录 学习笔记:CentOS7学习之二十二: 结构化命令case和for.while循环 22.1 流程控制语句:case 22.2 循环语句 22.1.2 for-do-done 22.3 whil ...

  3. LC 599. Minimum Index Sum of Two Lists

    题目描述 Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of fav ...

  4. FastAdmin

    FastAdmin是一款基于ThinkPHP5+Bootstrap的极速后台开发框架. 感觉挺好用的. 开发文档https://doc.fastadmin.net/docs/index.html 根据 ...

  5. python以不同方式打印输出九九乘法表

    参考:http://www.cnblogs.com/suiy-160428/p/5594389.htmlpython输出 9*9 乘法口诀表 矩形输出九九乘法表: for i in range(1,1 ...

  6. 安装 Dashboard 插件

    Kubernetes Dashboard 是 k8s集群的一个 WEB UI管理工具,代码托管在 github 上,地址:https://github.com/kubernetes/dashboard ...

  7. 使用python django快速搭建微信公众号后台

    前言 使用python语言,django web框架,以及wechatpy,快速完成微信公众号后台服务的简易搭建,做记录于此. wechatpy是一个python的微信公众平台sdk,封装了被动消息和 ...

  8. 8-MySQL DBA笔记-测试基础

    第三部分 测试篇 测试需要掌握的知识面很广泛,本篇的关注点是数据库的性能测试和压力测试,对于其他领域的测试,由于涉猎不多,笔者就不做叙述了.DBA的工作职责之一就是评估软硬件,这往往是一项很耗时的工作 ...

  9. tinymce富文本是在modal框中弹出显示的问题

    记录一下,在用tinymce富文本的时候,由于是用在modal 上的,始终无法获取焦点,后来才发现问题出在tinymce在modal前创建了,所以导致这个问题,解决方案就是用 v-if="v ...

  10. 【QT学习笔记】二、信号槽和自定义信号槽

    1. 信号槽 int main(int argc, char *argv[]) { QApplication app(argc, argv); QPushButton button("Qui ...