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

📄 filedownload.java

📁 java阿里巴巴代码
💻 JAVA
字号:
/**
 * @(#) FileDownload.java
 * @version 1.0.1
 */

package tools.util;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;

public class FileDownload extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void destroy() {
    }

    public void doGet(HttpServletRequest req, HttpServletResponse res) 
                        throws ServletException, java.io.IOException  {
		
		/**
		 * This parameter should be got from input
		 */
		int filedecode = 0;
		if( req.getParameter("FileDecode") != null)
			filedecode = Integer.parseInt(req.getParameter("FileDecode").toString());
		else
			filedecode = 1;

		String strFullPath = null;
		if( req.getParameter("FullFileName") != null)
			strFullPath = req.getParameter("FullFileName").toString();
		else
			return;
		
		if (filedecode == 1) strFullPath = Decode.Decrypt(strFullPath);
		String strFileName = strFullPath.substring(strFullPath.lastIndexOf(File.separator)+1,strFullPath.length());
		int blockSize = 6000;
		File file = new File(strFullPath);
		if(file.exists() && file.isFile()){
			FileInputStream fileIn = new FileInputStream(file);
			long fileLen = file.length();
			
			int readBytes = 0;
			int totalRead = 0;
			byte b[] = new byte[blockSize];
		
			res.setContentType("application/x-something");
			res.setContentLength((int)fileLen);
			res.setHeader("Content-Disposition", "attachment; filename=" + strFileName);
		
			while((long)totalRead < fileLen)
			{
				readBytes = fileIn.read(b, 0, blockSize);
			    totalRead += readBytes;
			    res.getOutputStream().write(b, 0, readBytes);
			}
			fileIn.close();
		}
		return;
    }
}

⌨️ 快捷键说明

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