Struts2的文件上传和下载(1)单文件上传

在struts2中集成fileuoload功能,因为在导入的jar包中包含了common-fileipload.jar文件
在struts2中的interceptor 中有一个fileupload拦截器,他的主要功能就是完成文件上传。

注意事项

  1. method=post
  2. 所有组件必须有name
  3. encType=multipart/form-data

Struts2上传步骤

  1. 在页面上创建表单
  2. 创建Action,在action中定义三个变量用来接收上传信息
  3. 使用FileUtils的copyFile方法完成文件复制操作

FileUploadAction.java

package com.qwl.action;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
	private static final long serialVersionUID=1L;
	//提交过来的文件
	private File file;
	//提交过来的file的名字
	private String fileFileName;
	//提交过来的file的类型
	private String fileContentType;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String execute()throws Exception{
		/*System.out.println(file.getCanonicalPath());
		System.out.println(this.fileContentType);
		System.out.println(this.fileFileName);*/
		
		//流操作
		/*//文件输入流
		InputStream is=new FileInputStream(file);
		//设置文件的保存目录
		String uploadPath=ServletActionContext.getServletContext().getRealPath("/upload");
		//设置目标文件
		File toFile=new File(uploadPath,this.getFileFileName());
		//文件输出流
		OutputStream os=new FileOutputStream(toFile);
		byte[] buffer=new byte[1024];
		int length=0;
		//读取file文件输出到toFile文件中
		while(-1!=(length=is.read(buffer,0,buffer.length)))
		{
			os.write(buffer);
		}
		//关闭输入流和输出流
		is.close();
		os.close();*/
		writeFile();
		return SUCCESS;
	}
	public void writeFile() throws IOException{
		FileUtils.copyFile(file,new File("/upload",fileFileName));
	}
	
}

}

fileUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
	<s:form action="fileUpload" method="post" enctype="multipart/form-data">
		<s:file name="file" label="文件上传"></s:file>
		<s:submit value="上传"/>
		<s:reset value="重置"/>
	</s:form>
	
</body>
</html>

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
	<s:form action="fileUpload" method="post" enctype="multipart/form-data">
		<s:file name="file" label="文件上传"></s:file>
		<s:submit value="上传"/>
		<s:reset value="重置"/>
	</s:form>
	
</body>
</html>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC  
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"  
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
	<package name="struts2" namespace="/" extends="struts-default">
		<action name="fileUpload" class="com.qwl.action.FileUploadAction">
			<result name="success">/result.jsp</result>
		</action>
	</package>
</struts>    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值