import java.io.Closeable; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; import com.odianyun.cc.client.exception.OccClientRequestException; import com.odianyun.cc.model.dto.OccConfigDownloadDTO; public class OccRequestUtils { private final static Logger logger = LoggerFactory.getLogger(OccRequestUtils.class); private static int BUFFER_SIZE = 4096;//缓冲区大小 /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,参数为JSON格式。 * @param dir * 下载文件存储目录 * @return 所代表远程资源的响应结果 */ public static OccConfigDownloadDTO sendPost(String url, String param,String dir) { InputStreamReader isr = null; FileWriter fw = null; char[] buf = new char[BUFFER_SIZE]; int size = 0; String groupPath = null; String fileName = null; String fileVersion = null; File proFile = null; try { //发送请求 HttpURLConnection connection = sendParam(url,param); //读取Properties文件流 isr = new InputStreamReader(connection.getInputStream(),"UTF-8"); //从头中拿到文件相关属性 groupPath = connection.getHeaderField("groupPath"); Assert.notNull(groupPath,"Failed to obtain parameters from OCC-Server,groupPath is required; maybe record not found."); fileName = connection.getHeaderField("fileName"); Assert.notNull(fileName,"Failed to obtain parameters from OCC-Server,fileName is required; maybe record not found."); fileVersion = connection.getHeaderField("fileVersion"); Assert.notNull(fileName,"Failed to obtain parameters from OCC-Server,fileVersion is required; maybe record not found."); proFile = new File(System.getProperty("global.config.path") + File.separator + dir + File.separator + groupPath , fileName); if (proFile.exists()){ proFile.delete(); }else{ proFile.getParentFile().mkdirs(); } fw = new FileWriter(proFile); while ((size = isr.read(buf)) != -1){ fw.write(buf,0,size); } fw.flush(); } catch (Exception e) { throw new OccClientRequestException(e.getMessage(),e); } //使用finally块来关闭输出流、输入流 finally{ close(isr,fw); } return new OccConfigDownloadDTO(proFile,groupPath, fileName, fileVersion); } private static HttpURLConnection sendParam(String url,String param) throws IOException { URL realUrl = new URL(url); // 打开和URL之间的连接 HttpURLConnection connection = (HttpURLConnection)realUrl.openConnection(); // 设置通用的请求属性 connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type","application/json;charset=utf-8"); connection.connect(); PrintWriter out = null; try { // 获取URLConnection对象对应的输出流 out = new PrintWriter(connection.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); }finally { if (out != null){ out.close(); } } return connection; } private static void close(Closeable... closeAbles){ if (closeAbles == null || closeAbles.length <= 0) { return; } for (Closeable closeAble : closeAbles) { if (closeAble != null) { try { closeAble.close(); } catch (IOException e) { logger.error(e.getMessage(), e); } } } } }
HttpConnection下载文件
猜你喜欢
转载自jdkleo.iteye.com/blog/2300914
今日推荐
周排行