struts2入门教程三(上传与下载)(三)

2014-11-24 02:01:30 · 作者: · 浏览: 2
ork2.ActionSupport; public class UploadAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private String[] titles; private File[] upload; private String[] uploadContentType; private String[] uploadFileName; private String savePath; private String allowTypes; public String upload() throws Exception { if (upload.length <= 0) { return null; } for (int i = 0; i < upload.length; i++) { FileInputStream fis = new FileInputStream(getUpload()[i]); System.out.println(getUploadFileName()); FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName()[i]); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); fis.close(); } return SUCCESS; } public boolean check(String type){ String[] types=allowTypes.split(","); for (String s: types) { if(s.equals(type)){ return true; } } return false; } public void validate(){ for (String Type : uploadContentType) { boolean b=check(Type); if(!b){ // 添加FieldError addFieldError("upload", "您要上传的文件类型不正确!"); } } } public String[] getTitles() { return titles; } public void setTitles(String[] titles) { this.titles = titles; } 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 getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public void setSavePath(String savePath) { this.savePath = savePath; } // 设计校验类型的 注入 public void setAllowTypes(String allowTypes) { this.allowTypes = allowTypes; } }
struts.xml
  


  
  
	
    
     
     /uploadFiles 
     image/png,image/gif,image/jpg,image/jpeg 
     
       /succ.jsp 
      
     
      /index.jsp
      
     
   


  
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>



  
    
    My JSP 'index.jsp' starting page
	
  
  
  
  
  
   
  
文件名:
文件:

文件名:
文件:

下载

struts的下载方式

struts2 的文件下载

java 代码
package action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class DownAction extends ActionSupport {

	//该属性可以在配置文件中动态指定该属性值
		private String inputPath;
		//依赖注入该属性值的setter方法
		public void setInputPath(String value)
		{
			inputPath = value;
		}
		/*
		定义一个返回InputStream的方法,
		该方法将作为被下载文件的入口,
		且需要配置stream类型结果时指定inputName参数,
		inputName参数的值就是方法去掉get前缀、首字母小写的字符串
		*/
		public InputStream getTargetFile() throws Exception 
		{
			//ServletContext提供getResourceAsStream()方法
			//返回指定文件对应的输入流 
			return ServletActionContext.getServletContext()
				.getResourceAsStream(inputPath);
		}
	
}

struts.xml
配置一个action
  
		
   \uploadFiles\扑克.png
		
		
    
    image/png 
    targetFile 
    4096 
    filename="1110.jpg" 
   
		
   
     /index.jsp 
   
		
  

下载页面
简单的超链接下载
111
222

  

简单的超链接下载

只需要基本的一个访问链接即可