`
wangyujie
  • 浏览: 53901 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

Struts2的文件上传

阅读更多

文件的上传处理

1、 单个文件上传:

第一步:把form表的enctype设置为:“multipart/form-data“和method="post“ ,如下:

<s:form action="./insertShop" method="post“ enctype="multipart/form-data">

<s:textfield name="name" label="商品的名称"></s:textfield>

<s:file name="picture" label="商品的图片"></s:file>

<s:submit value="提交"></s:submit>

</s:form>

第二步骤:实现文件上传的Action类

package com.shop.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.sql.Timestamp;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ShopAction extends ActionSupport {

private String name;// 商品的名称

private File picture;// 和页面上<s:file name="picture" label="商品的图片"></s:file>

// name名字一致

private String pictureFileName;// 用来封装文件的名字,命名规范***FileName,***是页面name的值与File的名称一致

private String pictureContentType;// 用来封装文件的类型,命名规范***ContentType,***是页面name的值与File的名称一致

private String savePath;// 接受依赖注入的属性值,即文件上传的路径

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public File getPicture() {

return picture;

}

public void setPicture(File picture) {

this.picture = picture;

}

public String getPictureFileName() {

return pictureFileName;

}

// 上面的命名规范主要是生成set,get方法 生成以后即可以更改属性的名称,一般不更改

public void setPictureFileName(String pictureFileName) {

this.pictureFileName = pictureFileName;

}

public String getPictureContentType() {

return pictureContentType;

}

public void setPictureContentType(String pictureContentType) {

this.pictureContentType = pictureContentType;

}

public String getSavePath() {

// 更改商品图片保存的路径

return ServletActionContext.getRequest().getRealPath(savePath);

}

public void setSavePath(String savePath) {

this.savePath = savePath;

}

public String insert() {

try {

// 创建文件的目录

File spath = new File(getSavePath());

if (!spath.exists()) {

System.out.println("创建文件的路径.......");

spath.mkdirs();

}

// 以服务器的文件保存地址和原文件名称建立上传文件的输出流

FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"

+ getPictureFileName());

// 创建文件读取的输入流,读取本机的

FileInputStream fis = new FileInputStream(getPicture());

int len = 0;

while ((len = fis.read()) != -1) {

fos.write(len);

}

catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return SUCCESS;

}

}Struts.xml文件的配置:

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<package name="upl" extends="struts-default" namespace="/">

<action name="insertShop" class="com.shop.action.ShopAction"

method="insert">

<!-- 添加文件上传的拦截器  -->

<interceptor-ref name="fileUpload">

    <!-- 设置文件的内容类型 -->

<param name="allowedTypes">image/bmp,image/x-png,image/gif</param>

<!-- 设置文件内容的最大字节数 -->

<param name="maximumSize">20000000</param>

</interceptor-ref>

<!-- 添加默认的拦截器  -->

<interceptor-ref name="defaultStack"></interceptor-ref>

<!-- savePath不能放置到interceptor中 -->

<param name="savePath">/upload</param>

<result name="success">/shop/showShop.jsp</result>

<result name="input">/shop/insertShop.jsp</result>

</action>

</package>

</struts>    

2、 多个文件的上传

第一步骤:添加视图界面

<h1>多个文件的上传</h1>

     <s:form action="./uploads.action" method="post" enctype="multipart/form-data">

       <s:textfield name="title" label="文件标题"></s:textfield>

       <s:file name="upload" label="第一个文件"></s:file>

       <s:file name="upload" label="第二个文件"></s:file>

       <s:file name="upload" label="第三个文件"></s:file>

       <s:file name="upload" label="第四个文件"></s:file>

       <s:submit value="提交"></s:submit>

     </s:form>

 第二步骤:实现多个文件上传的Action

package com.redarmy.action;

import java.io.BufferedInputStream;<sp

分享到:
评论

相关推荐

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2文件上传

    struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    Struts2文件上传

    Struts2文件上传

    Struts2 文件上传

    Struts2 文件上传

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传与下载

    struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!

    struts2 文件上传功能

    Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

Global site tag (gtag.js) - Google Analytics