HttpIOUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package com.muzhi.zgd.http;
  2. import java.io.*;
  3. import java.net.HttpURLConnection;
  4. import java.net.URLConnection;
  5. /**
  6. *
  7. * @className : HttpIOUtils
  8. * @description : 扩展org.apache.commons.io.IOUtils工具对象
  9. * @date : 2017年6月13日 下午9:20:40
  10. * @version V1.0
  11. */
  12. public abstract class HttpIOUtils extends org.apache.commons.io.IOUtils{
  13. public static final int BUFFER_SIZE = 1024 * 4;
  14. public static void closeConnect(final URLConnection conn) {
  15. try {
  16. if (conn != null) {
  17. if (conn instanceof HttpURLConnection) {
  18. ((HttpURLConnection) conn).disconnect();
  19. }
  20. }
  21. } catch (final Exception ioe) {
  22. // ignore
  23. }
  24. }
  25. //---------------------------------------------------------------------
  26. // Stream Warp methods for java.io.InputStream / java.io.OutputStream
  27. //---------------------------------------------------------------------
  28. public static InputStream toBufferedInputStream(File localFile,int bufferSize) throws IOException {
  29. // 包装文件输入流
  30. return toBufferedInputStream(new FileInputStream(localFile),bufferSize);
  31. }
  32. public static InputStream toBufferedInputStream(InputStream input) throws IOException {
  33. if(isBuffered(input)){
  34. return (BufferedInputStream) input ;
  35. }else{
  36. return org.apache.commons.io.output.ByteArrayOutputStream.toBufferedInputStream(input);
  37. }
  38. }
  39. public static InputStream toBufferedInputStream(InputStream input,int bufferSize) throws IOException {
  40. if(isBuffered(input)){
  41. return (BufferedInputStream) input ;
  42. }else{
  43. if (bufferSize > 0) {
  44. return new BufferedInputStream(input, bufferSize);
  45. }
  46. return new BufferedInputStream(input);
  47. }
  48. }
  49. public static OutputStream toBufferedOutputStream(OutputStream output) throws IOException {
  50. if(isBuffered(output)){
  51. return (BufferedOutputStream) output ;
  52. }else{
  53. return toBufferedOutputStream(output , BUFFER_SIZE);
  54. }
  55. }
  56. public static OutputStream toBufferedOutputStream(OutputStream output,int bufferSize) throws IOException {
  57. if(isBuffered(output)){
  58. return (BufferedOutputStream) output ;
  59. }else{
  60. if (bufferSize > 0) {
  61. return new BufferedOutputStream(output, bufferSize);
  62. }
  63. return new BufferedOutputStream(output);
  64. }
  65. }
  66. public static boolean isBuffered(InputStream input) {
  67. return input instanceof BufferedInputStream;
  68. }
  69. public static boolean isBuffered(OutputStream output) {
  70. return output instanceof BufferedOutputStream;
  71. }
  72. public static BufferedReader toBufferedReader(InputStream input){
  73. return new BufferedReader(new InputStreamReader(input));
  74. }
  75. public static BufferedWriter toBufferedWriter(OutputStream output){
  76. return new BufferedWriter(new OutputStreamWriter(output));
  77. }
  78. public static boolean isPrint(OutputStream output) {
  79. return output instanceof PrintStream;
  80. }
  81. public static InputStream toByteArrayInputStream(byte[] inputBytes){
  82. return new ByteArrayInputStream(inputBytes);
  83. }
  84. public static InputStream toByteArrayInputStream(String text) {
  85. return toByteArrayInputStream(text.getBytes());
  86. }
  87. public static DataInputStream toDataInputStream(InputStream input) {
  88. return isDataInput(input) ? (DataInputStream) input : new DataInputStream(input);
  89. }
  90. private static boolean isDataInput(InputStream input) {
  91. return input instanceof DataInputStream;
  92. }
  93. public static DataOutputStream toDataOutputStream(OutputStream output){
  94. return isDataOutput(output) ? (DataOutputStream) output : new DataOutputStream(output);
  95. }
  96. private static boolean isDataOutput(OutputStream output) {
  97. return output instanceof DataOutputStream;
  98. }
  99. public static FileInputStream toFileInputStream(File file) throws IOException {
  100. return new FileInputStream(file);
  101. }
  102. /**
  103. * 获得一个FileOutputStream对象
  104. * @param file
  105. * @param append : true:向文件尾部追见数据; false:清楚旧数据
  106. * @return
  107. * @throws FileNotFoundException
  108. */
  109. public static FileOutputStream toFileOutputStream(File file,boolean append) throws FileNotFoundException {
  110. return new FileOutputStream(file, append);
  111. }
  112. public static PrintStream toPrintStream(File file) {
  113. FileOutputStream stream = null;
  114. try {
  115. stream = new FileOutputStream(file,true);
  116. return toPrintStream(stream);
  117. } catch (FileNotFoundException e) {
  118. throw new RuntimeException(e);
  119. }
  120. }
  121. public static PrintStream toPrintStream(OutputStream output) {
  122. return isPrint(output) ? (PrintStream) output : new PrintStream(output);
  123. }
  124. public static String toInputText(InputStream input, String charset) throws IOException {
  125. BufferedReader reader = null;
  126. try {
  127. StringBuffer result = new StringBuffer();
  128. reader = new BufferedReader(new InputStreamReader(input, charset));
  129. String strRead = null;
  130. int row = 0;
  131. while ((strRead = reader.readLine()) != null) {
  132. // 多行数据,且当前行读取到数据,则在上一行之后添加换行符
  133. if (row > 0 && strRead != null && strRead.length() > 0) {
  134. result.append("\r\n");
  135. }
  136. result.append(strRead);
  137. row++;
  138. }
  139. return result.toString();
  140. } finally {
  141. // 释放资源
  142. HttpIOUtils.closeQuietly(input);
  143. HttpIOUtils.closeQuietly(reader);
  144. }
  145. }
  146. }