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); } } } } }
相关推荐
android中利用httpconnection上传文件。可以选择要上传文件的路径,上传网址
NULL 博文链接:https://wangxc.iteye.com/blog/591160
Android文件断点下载,使用HttpConnection
利用HTTPConnection类设置文件上传,这里没有搭建服务器端代码,因为太简单了...
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:191) at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80) at ...
UploadFileUtil工具类可以针对各种文件(图片、声音)进行httpConnection 上传
[+]针对某些超时的情况,注释掉 httplib.HTTPConnection._http_vsn = 10 和httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'这两行再测试一遍,因为有的可能不支持HTTP/1.0的协议。 [+]增加linux和win的可执行文件...
下面小编就为大家带来一篇利用HttpUrlConnection 上传 接收文件的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
3、 业务逻辑代码与布局文件分别是MainActivity.java 、DetailActivity.java、TQYBInfo.java、XmlInfoUtil.java和activity_main.xml、a ctivity_detail.xml、listview_detail.xml。 4、 在 AndroidManifest.xml 中...
软件支持高效、快速的数据存储方式,包括快速数据存储方式SharedPreferences、文件存储和轻量级关系数据块SQLite,可软件可以使用适合的方法对数据进行进程保存和访问。 假设和约束(依赖) 软件必须在安卓系统下才...
软件支持高效、快速的数据存储方式,包括快速数据存储方式SharedPreferences、文件 存储和轻量级关系数据块SQLite,可软件可以使用适合的方法对数据进行进程保存和访 问。 4. 假设和约束(依赖) 软件必须在安卓系统...