当前位置: 开源爱好者 » 编程教程 » Java教程 » 正文

Java通过HTTP下载文件并保存

Java通过HTTP下载文件并保存,通过HTTP保存文件:

package com.ehm.hes.report.ftp;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.healthin.uc.action.ReportAction;
public class HttpSaveFile {
private static final Logger logger = LoggerFactory.getLogger(ReportAction.class);
	/**
	 * @param fileName 要写入临时本地的文件名
	 * @param tempFilePath 要写入到临时本地文件夹
	 * @param url 要获取流的http的外网路径
	 * @param folder 用于区别不同体检中心生成报告的文件夹
	 */
	public static void getHttpSaveFile(String fileName,
			String tempFilePath,
			String url,
			String folder){
		String httpStr = "";
		try {
			// 地址:http://127.0.0.1:8080/EastHealthMembersSystem/admin/mInfo/findmemberRegister.xhtml
			url = new String(httpStr.getBytes("ISO-8859-1"), "UTF-8");
			logger.debug("浏览器地址:" + url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//此处为生成文件的文件名,这里要从后台获得
		String filepath = tempFilePath + fileName;
		InputStream in = HttpSaveFileUilts.getInputStram(url);
		HttpSaveFileUilts.WriteFile(in, filepath);
		logger.debug("{}", "文件写入成功!");
		String localFilePath = null;
		String ip = null;
		int port = 0;
		String uid = null;
		String pass = null;
		ReportFtp.uploadFtp(fileName, folder, localFilePath, ip, port, uid, pass);
	}
}

CopyRight 2015© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图