• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Java从网上下载文件的代码

JAVA相关 水墨上仙 2577次浏览

Java从网上下载文件的代码
转自:http://blog.csdn.net/heng_ji/article/details/8481922

DownLoadUtil.java

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class DownLoadUtil {
	private static Logger logger = Logger.getLogger(DownLoadUtil.class);
	/**
	 * 下载文件
	 * @param name 用户下载的文件名(*****.***)
	 * @param filePath 文件路径
	 * @param response 
	 * @param fileType 文件类型
	 * @return
	 * @throws Exception
	 */
	public static boolean downLoadFile(String name,String filePath,
			HttpServletResponse response, String fileType)
			throws Exception {
		logger.info("start invoke downLoadFile,[filePath:"+filePath+" , fileType:"+fileType+"]");
		File file = new File(filePath);
		//设置文件类型
		if("pdf".equals(fileType)){
			response.setContentType("application/pdf");
		}else if("xls".equals(fileType)){
			response.setContentType("application/msexcel");
		}else if("doc".equals(fileType)){
			response.setContentType("application/msword");
		}
		response.setHeader("Content-Disposition", "attachment;filename=\""
				+ new String(name.getBytes("GB2312"), "ISO8859-1") + "\"");
		//response.setHeader("Content-Disposition", "attachment;filename=\""+ URLEncoder.encode(name, "UTF-8")+ "\"");
		response.setContentLength((int) file.length());
		byte[] buffer = new byte[4096];// 缓冲区
		BufferedOutputStream output = null;
		BufferedInputStream input = null;
		try {
			output = new BufferedOutputStream(response.getOutputStream());
			input = new BufferedInputStream(new FileInputStream(file));
			int n = -1;
			while ((n = input.read(buffer, 0, 4096)) > -1) {
				output.write(buffer, 0, n);
			}
			output.flush();
			response.flushBuffer();
		} catch (Exception e) {
			logger.error("exception when invoke downLoadFile",e);
			throw e;
		} finally {
			if (input != null)
				input.close();
			if (output != null)
				output.close();
		}
		logger.info("end invoke downLoadFile!");
		return true;
	}
}

调用&nbspDownLoadUtil.java

public ActionForward downLoadExcelModel(ActionMapping actionMapping,
   ActionForm actionForm, HttpServletRequest request,
   HttpServletResponse response) throws Exception {
  String busType = request.getParameter("busType");
  String path = request.getSession().getServletContext().getRealPath(
    "/resources");
  String name = null;        
  String fileName = null;    //服务器上文件名
    fileName = "xxxl.xls";
    name = "xxxx.xls";
  }
  String filePath = path + System.getProperty("file.separator") + fileName;
  DownLoadUtil.downLoadFile(name,filePath, response, "xls");
  return null;
 }


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Java从网上下载文件的代码
喜欢 (0)
加载中……