基于struts的文件上传与下载

上传

一、首先在tomcat服务器中的webapps文件夹中的子文件夹,例如updownload项目中建一个upload文件夹,为了保存上传的文件。

二、准备upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>struts上传</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="/strutsday6/upload.action" method="post" enctype="multipart/form-data">
    	<input type="file" name="file">
    	<input type="submit" value="上传">
    </form>
  </body>
</html>

其中form表单中的method必须为post,并且必须添加一个属性:enctype="multipart/form-data",如果不添加,服务器端是不会认识所提交的内容;并且input中的name属性的名称要和下面action中的私有成员保持一致。

三、编写UploadAction

package zpark.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{
	private File file;	//文件
	
	private String fileFileName;	//文件原始文件名
	
	private String fileContentType;	//文件类型
	
	public String upload(){
		InputStream is = null;
		OutputStream os = null;
		
		try{
			//得到当前路径
			ServletContext sc = ServletActionContext.getServletContext();
			String path = sc.getRealPath("/upload");
			
			//准备输入输出流
			is = new FileInputStream(file);
			os = new FileOutputStream(path + "/" + fileFileName);
			
			byte[] buf = new byte[1024 * 16];
			int len = -1;
			
			while(true){
				len = is.read(buf);
				
				if(len == -1) break;
				
				os.write(buf, 0, len);
			}
			
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(os != null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return "success";
	}

	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;
	}
}

其中,file与upload.jsp中input的属性name的值保持一致,并且文件原始名也就是要上传的文件原本的名称,它的格式为文件+FileName固定写法,例如fileFileName;文件类型也如此,为fileContentType

四、配置struts.xml

 <package name="p1" extends="struts-default" namespace="/">
    	<action name="upload" class="zpark.action.UploadAction" method="upload">
    		<result name="success">/success.jsp</result>
    	</action>
    </package>
五、测试访问

首先进入Upload.jsp页面,选择要上传的文件之后,点击上传按钮,进入UploadAction中的upload方法,上传成功后由succes.jsp页面显示“上传成功!”字样,同时,查看tomcat的webapps中应用程序的文件夹中的upload文件夹出现了上传的文件,此时上传成功!


下载

对于下载,为了方便演示下载的流程,我已经把要下载的文件写死在代码中。

一、写DownloadAction类

package zpark.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport{
	private String contentType="application/octet-stream"; //响应类型
	
	private String contentDisposition = "attachment;filename=\"1.jpg\"";	//设置请求头,并设置下载后的文件名叫1.jpg
	
	private Long contentLength;	//要下载的文件的大小
	
	private InputStream inputStream;	//输入流
	
	public String execute() throws Exception{
		ServletContext sc = ServletActionContext.getServletContext();
		String path = sc.getRealPath("/upload/psb.jpg");
		inputStream = new FileInputStream(path);
		contentLength = new File(path).length();
		
		return "success";
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getContentDisposition() {
		return contentDisposition;
	}

	public void setContentDisposition(String contentDisposition) {
		this.contentDisposition = contentDisposition;
	}

	public Long getContentLength() {
		return contentLength;
	}

	public void setContentLength(Long contentLength) {
		this.contentLength = contentLength;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	
	
}

二、配置struts.xml

<package name="p1" extends="struts-default" namespace="/">
    	<action name="upload" class="zpark.action.UploadAction" method="upload">
    		<result name="success">/success.jsp</result>
    	</action>
    	
    	<action name="download" class="zpark.action.DownloadAction">
    		<result name="success" type="stream"></result>
    	</action>
    </package>

三、测试访问

由于将下载的文件写死在代码中,所以这个例子访问的方法为在地址栏写http://localhost:8089/download.action,访问后,谷歌浏览器下会直接下载文件,IE浏览器下会弹出一个对话框显示“打开”“保存”“取消”,根据需求选择即可。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值