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

分享到:
评论

猜你喜欢

转载自wangyujie.iteye.com/blog/1051213