⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filedownaction.java

📁 功能是公安和学校的管理系统,用struts+hibernate+spring等匡架组成,在eclepse就能编译
💻 JAVA
字号:
package com.comingnet.action;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class FiledownAction extends ActionSupport {
	
	private String  path,name;
	@SuppressWarnings("finally")
	public String execute() throws IOException{
			  
		name= new String(name.getBytes("ISO-8859-1"), "UTF-8");
		
		System.out.println("name-->"+name);
		
		
		  HttpServletResponse response = ServletActionContext.getResponse(); 
		      response.reset();//可以加也可以不加
		  response.setContentType("application/x-download");
		  name  = URLEncoder.encode(name,"UTF-8");
		  response.addHeader("Content-Disposition","attachment;filename="+name);
		  OutputStream	outp=null;
		    FileInputStream in = null;
		    try
		     {
		    	outp=response.getOutputStream();
			   in = new FileInputStream(getPath());
		        byte[] b = new byte[1024];
		        int i = 0;
		        while((i = in.read(b)) > 0)
		        {
		           outp.write(b, 0, i);
		        }
		        outp.flush();
		    }
		    catch(Exception e)
		    {
		      //  System.out.println("Error!");
		        e.printStackTrace();
		    }
		    finally
		    {
		        if(in != null)
		        {
		            in.close();
		            in = null;
		        }
		        if(outp != null)
		        {
		            outp.close();
		            outp = null;
		        }
		
		return SUCCESS;	
	}
	
}
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}


	public void setPath(String path) {
		this.path = path;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -