Struts2框架——文件上传下载

前台页面:

    <%@taglib uri="/struts-tags" prefix="s" %>
     <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Struts2配置文件设置:

<?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>
   <!-- 文件上传 常量设置 -->
  <constant name="struts.mulitipart.maxSize" value="500000000"></constant>
  <package name="uppk" extends="json-default,struts-default" namespace="/">
       <action name="fileUP_*" method="{1}" class="com.zgq.action.UploadFileAction" >
           
           <!-- 配置上传文件类型 -->
            <param name="savePath">/place</param>
                  <interceptor-ref name="fileUpload">
                       <param name="allowedTypes">text/plain,image/ief,image/jpeg</param>
                  </interceptor-ref>
                  <interceptor-ref name="defaultStack"/>
                   <result name="success">/index.jsp</result>
                   
                   
                
       </action>
       


  <action name="downFile_*" class="com.zgq.action.UploadFileAction" method="{1}">
			 <result type="stream">
			        <!--文件类型 -->
			        <param name="contentType">text/plain</param>
			        <!--指定文件名 -->
			        <param name="contentDisposition">
			            attachment;filename=${filename}
			        </param>
			        <!--输入流 -->
			        <param name="inputName">downloadFile</param>
			    </result>
			    <result name="list">/index.jsp</result>
		</action>
		
		<action name="user_*" class="com.zgq.action.UserAction" method="{1}">
		         <result name="success">/userInfo.jsp</result> 
		         <result name="resAjax" type="json">  
		                <param name="excludeNullProperties">true</param>  
		                <param name="root">userEnt</param>
		                <param name="contentType">text/html</param>
                </result> 
                
		</action>
		
  </package>
  
</struts>    

后台业务逻辑:

package com.zgq.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadFileAction extends ActionSupport {
      
	//封装文件标题请求参数的属性
		//private String title;
		//封装上传文件内容的属性
		private File upload;
		//封装上传文件类型的属性
		private String uploadContentType;
		//封装上传文件名的属性
		private String uploadFileName;
		//在服务器存放用户上传文件的路径,直接在struts.xml 文件中配置的属性
		private String savePath;
		//封装允许上传文件的文件类型
		private String allowTypes;
		//封装允许上传文件的大小值
		//private Long maximumSize;
	
		
		public File getUpload() {
			return upload;
		}

		public void setUpload(File upload) {
			this.upload = upload;
		}

		public String getUploadContentType() {
			return uploadContentType;
		}

		public void setUploadContentType(String uploadContentType) {
			this.uploadContentType = uploadContentType;
		}

		public String getUploadFileName() {
			return uploadFileName;
		}

		public void setUploadFileName(String uploadFileName) {
			this.uploadFileName = uploadFileName;
		}

		public String getAllowTypes() {
			return allowTypes;
		}

		public void setAllowTypes(String allowTypes) {
			this.allowTypes = allowTypes;
		}

		//获取上传文件的保存位置
		public String getSavePath() {
			return ServletActionContext.getServletContext().getRealPath(savePath);
//		return savePath;
		}
		public void setSavePath(String savePath) {
			this.savePath = savePath;
		}


		public String uploadFile() throws IOException{
			
			// 以文件保存路径 + 文件名建立上传文件的输出流存放点
 			FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
			//以用户的上传文件建立上传文件输出流对象
			FileInputStream fis = new FileInputStream(getUpload());
			//设置以字节格式存放,以及每个字节的大小
			byte[] buffer = new byte[1024];
			//定义一个存放文件字节数的变量
			int len = 0;
			//把用户上传文件以字节流的方式存放到服务器中
			while((len = fis.read(buffer))> 0 ) {
				fos.write(buffer,0,len);
			}
			//关闭流
			fos.close();
			fis.close();
			
			ActionContext.getContext().put("returnInfo", "成功");
			return SUCCESS;

		}
		
		
		
		
		private File[] files;
		
        public File[] getFiles() {
			return files;
		}
		public void setFiles(File[] files) {
			this.files = files;
		}
		public String list() throws IOException{
			//获取路径
			String filePath = ServletActionContext.getServletContext().getRealPath("/")+"place/";
			//得到文件夹
			File fp = new File(filePath);
			//取出文件数组数据
		     files = fp.listFiles();
			//定义一个集合存放遍历后的数据
			ArrayList<String> list = new ArrayList<String>();
			for (File f : files) {
				list.add(f.getName());
			}
			ActionContext.getContext().put("list", list);
			System.out.println(list);
			return "list";
			
		}
		
		
		private String fileName;

		//设置文件名称
		public String getFileName() throws UnsupportedEncodingException{
			/*//设置响应内容格式
			ServletActionContext.getResponse().setHeader("charset","ISO8859-1");*/
			return fileName;
		}

		public void setFileName(String dfName) {
			this.fileName = dfName;
		}
		//开始下载
		public InputStream getDownloadFile(){
			InputStream instrem = null;
			try {
				instrem = ServletActionContext.getServletContext().getResourceAsStream("place/"+fileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return instrem;
		}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值