当前位置:   article > 正文

从网络地址下载文件_下载文件网址怎么填

下载文件网址怎么填
  1. import javax.servlet.http.HttpServletResponse;
  2. import java.io.*;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.net.URLEncoder;
  6. public class HttpUtils {
  7. /**
  8. * 从网络Url中下载文件到本地磁盘
  9. *
  10. * @param urlStr
  11. * @param fileName
  12. * @param savePath
  13. * @throws IOException
  14. */
  15. public static void downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
  16. URL url = new URL(urlStr);
  17. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  18. //设置超时间为3秒
  19. conn.setConnectTimeout(30 * 1000);
  20. conn.setReadTimeout(60 * 1000);
  21. //得到输入流
  22. InputStream inputStream = conn.getInputStream();
  23. //获取自己数组
  24. byte[] getData = readInputStream(inputStream);
  25. //文件保存位置
  26. File saveDir = new File(savePath);
  27. if (!saveDir.exists()) {
  28. saveDir.mkdir();
  29. }
  30. File file = new File(saveDir + File.separator + fileName);
  31. FileOutputStream fos = new FileOutputStream(file);
  32. fos.write(getData);
  33. if (fos != null) {
  34. fos.close();
  35. }
  36. if (inputStream != null) {
  37. inputStream.close();
  38. }
  39. }
  40. /**
  41. * 从网络url下载excel
  42. * @param url
  43. * @param fileName
  44. * @param response
  45. * @throws Exception
  46. */
  47. public void downloadExcelByUrl(String url, String fileName, HttpServletResponse response) throws Exception {
  48. fileName = URLEncoder.encode(fileName, "UTF-8");
  49. response.setContentType("application/vnd.ms-excel;charset=utf-8");
  50. response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
  51. byte[] buff = new byte[1024];
  52. BufferedInputStream bis = null;
  53. OutputStream outputStream = null;
  54. InputStream inputStream = null;
  55. try {
  56. URL httpUrl = new URL(url);
  57. HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
  58. connection.setConnectTimeout(3000);
  59. connection.setReadTimeout(5000);
  60. outputStream = response.getOutputStream();
  61. inputStream = connection.getInputStream();
  62. bis = new BufferedInputStream(inputStream);
  63. int len;
  64. while ((len = bis.read(buff)) > 0) {
  65. outputStream.write(buff, 0, len);
  66. outputStream.flush();
  67. }
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. } finally {
  71. if (bis != null) {
  72. bis.close();
  73. }
  74. if (outputStream != null) {
  75. outputStream.close();
  76. }
  77. if (inputStream != null) {
  78. inputStream.close();
  79. }
  80. }
  81. }
  82. /**
  83. * 从输入流中获取字节数组
  84. *
  85. * @param inputStream
  86. * @return
  87. * @throws IOException
  88. */
  89. public static byte[] readInputStream(InputStream inputStream) throws IOException {
  90. byte[] buffer = new byte[1024];
  91. int len = 0;
  92. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  93. while ((len = inputStream.read(buffer)) != -1) {
  94. bos.write(buffer, 0, len);
  95. }
  96. bos.close();
  97. return bos.toByteArray();
  98. }
  99. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55416
推荐阅读
相关标签
  

闽ICP备14008679号