java struts上传_struts2单个文件上传的两种实现方式

通过2种方式模拟单个文件上传,效果如下所示

f225fd4ad32c9d879a6348685e34816b.png

开发步骤如下:

1、新建一个web工程,导入struts2上传文件所需jar,如下图

291dd789316351190855fe0ebd3392ff.png

目录结构

e953a87895535425d94f164bd9449111.png

2、新建Action

第一种方式

package com.ljq.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")

public class UploadAction extends ActionSupport{

private File image; //上传的文件

private String imageFileName; //文件名称

private String imageContentType; //文件类型

public String execute() throws Exception {

String realpath = ServletActionContext.getServletContext().getRealPath("/images");

//D:\apache-tomcat-6.0.18\webapps\struts2_upload\images

System.out.println("realpath: "+realpath);

if (image != null) {

File savefile = new File(new File(realpath), imageFileName);

if (!savefile.getParentFile().exists())

savefile.getParentFile().mkdirs();

FileUtils.copyFile(image, savefile);

ActionContext.getContext().put("message", "文件上传成功");

}

return "success";

}

public File getImage() {

return image;

}

public void setImage(File image) {

this.image = image;

}

public String getImageFileName() {

return imageFileName;

}

public void setImageFileName(String imageFileName) {

this.imageFileName = imageFileName;

}

public String getImageContentType() {

return imageContentType;

}

public void setImageContentType(String imageContentType) {

this.imageContentType = imageContentType;

}

}

第二种方式

package com.ljq.action;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")

public class UploadAction2 extends ActionSupport {

// 封装上传文件域的属性

private File image;

// 封装上传文件类型的属性

private String imageContentType;

// 封装上传文件名的属性

private String imageFileName;

// 接受依赖注入的属性

private String savePath;

@Override

public String execute() {

FileOutputStream fos = null;

FileInputStream fis = null;

try {

// 建立文件输出流

System.out.println(getSavePath());

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

// 建立文件上传流

fis = new FileInputStream(getImage());

byte[] buffer = new byte[1024];

int len = 0;

while ((len = fis.read(buffer)) > 0) {

fos.write(buffer, 0, len);

}

} catch (Exception e) {

System.out.println("文件上传失败");

e.printStackTrace();

} finally {

close(fos, fis);

}

return SUCCESS;

}

/**

* 返回上传文件的保存位置

*

* @return

*/

public String getSavePath() throws Exception{

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

}

public void setSavePath(String savePath) {

this.savePath = savePath;

}

public File getImage() {

return image;

}

public void setImage(File image) {

this.image = image;

}

public String getImageContentType() {

return imageContentType;

}

public void setImageContentType(String imageContentType) {

this.imageContentType = imageContentType;

}

public String getImageFileName() {

return imageFileName;

}

public void setImageFileName(String imageFileName) {

this.imageFileName = imageFileName;

}

private void close(FileOutputStream fos, FileInputStream fis) {

if (fis != null) {

try {

fis.close();

} catch (IOException e) {

System.out.println("FileInputStream关闭失败");

e.printStackTrace();

}

}

if (fos != null) {

try {

fos.close();

} catch (IOException e) {

System.out.println("FileOutputStream关闭失败");

e.printStackTrace();

}

}

}

}

struts.xml配置文件

/p>

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

/WEB-INF/page/message.jsp

/images

/WEB-INF/page/message.jsp

/upload/upload.jsp

image/bmp,image/png,image/gif,image/jpeg

1025956

上传表单页面

文件上传

enctype="multipart/form-data" method="post">

文件:

显示结果页面

上传成功

上传成功!

    <s:property%20value=">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值