当前位置: 代码迷 >> Web前端 >> 利用struts2上载缓存于内存中的文件
  详细解决方案

利用struts2上载缓存于内存中的文件

热度:82   发布时间:2012-11-22 00:16:41.0
利用struts2下载缓存于内存中的文件

因为工作需要,要把被下载的文件缓存在内从中供用户下载,提高响应速度,降低硬盘I/O负担.通过一段时间的资料查找终于将这个问题给搞定了.

1.将文件缓存与内存中,大致代码如下.

?

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;


public class GameFileCache {
	public static HashMap<String,byte[]> MAP_DOWNLOAD = new HashMap<String,byte[]>();
	
	/**
	 * 将文件以byte数组的方式缓存于内存中.因为byte[] 数组有长度限制,所以最大的缓存字节数为int类型的最大值
	 * @param filePath	文件路径
	 * @param key		byte数组在map中对应的key
	 */
	public static void readFileToMemory(String filePath,String key){
		File file = new File(filePath);
		Long fileLength = file.length();
		try {
			byte[] filecontent = new byte[fileLength.intValue()];
			DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
			in.read(filecontent);
			MAP_DOWNLOAD.put(key, filecontent);
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将byte数组转为inputstream
	 * @param in
	 * @return
	 */
	public static InputStream byteToInputStream(byte[] in){
		ByteArrayInputStream is = new ByteArrayInputStream(in);
		return is;
	}
		
}
?

?

2.配置struts的下载xml,这里只贴出下载类的配置.这里下载时候的文件名字是写的固定的,当然也可以写成动态的,至于怎么写成动态的,百度一下是很多的.

?

<result name="getGameFile" type="stream">
				<param name="contentType">application/octet-stream</param>
				<param name="inputName">inputStream</param>
				<param name="contentDisposition">attachment;filename="XXX.XX"</param>
				<param name="bufferSize">4096</param>
</result>

?

?3.配置完成以后贴出下载的action方法

?

import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class GameDownloadAction extends ActionSupport implements ServletContextAware,	ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	private String fileName;	//渠道名称
	private byte[] in;
	
	public String formateString(String str){
		if(null == str){
			return "";
		}
		return str.trim();
	}
	
	/**
	 * 下载文件
	 * @return
	 */
	public String getGameFile(){
		if(GameFileCache.MAP_DOWNLOAD.size() > 0){
			if(!fileName.isEmpty()){
				in = GameFileCache.MAP_DOWNLOAD.get(fileName);	//获取缓存中的文件
			}
		}
		return "getGameFile";
	}
	
	/**
	 * 将文件缓存到内存中,渠道名称就是缓存到内存中的文件key值.filepath就是文件在硬盘中的路径
	 */
	public void setGameFile(){
		String channle = formateString(request.getParameter("channel"));	//渠道名称
		String filePath = formateString(request.getParameter("file"));		//文件路径
		GameFileCache.readFileToMemory(filePath, channle);
	}

	public InputStream getInputStream() throws Exception{
		return GameFileCache.byteToInputStream(in);
	}
	
	public void setServletContext(ServletContext arg0) {
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	
}
?

?

通过以上三个步骤就可以实现将多个文件缓存在内存中,供用户下载了,这样下载的时候就不用去读硬盘了,直接从内存读取,这样可以提高系统相应速度.

第一次写博客,请多指教.

  相关解决方案