使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法

时间:2023-03-09 07:23:24
使用HttpURLConnection下载文件时出现 java.io.FileNotFoundException彻底解决办法

使用HttpURLConnection下载文件时经常会出现 java.io.FileNotFoundException文件找不到异常,下面介绍下解决办法

首先设置tomcat对get数据的编码:conf/server.xml

  1. <Connector port="8080" protocol="HTTP/1.1"
  2. connectionTimeout="20000"
  3. redirectPort="8443"
  4. <span style="color:#ff0000;"> URIEncoding="UTF-8"</span> />

其次对请求的文件名进行编码:

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.RandomAccessFile;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.net.URLEncoder;
  8. /**
  9. * 多线程下载
  10. * @author bing
  11. *
  12. */
  13. public class OmbDownloadOfThreadsUtil {
  14. private String urlPath ; // 资源网络路径
  15. private String targetFilePath ; // 所下载文件的保存路径
  16. private int threadNum ; // 启用多少条线程进行下载
  17. // 用于下载线程对象集合
  18. private DownloadThread[] downloadThreads ;
  19. // 要下载文件的大小
  20. private int fileSize ;
  21. public OmbDownloadOfThreadsUtil(String urlPath, String targetFilePath,
  22. int threadNum) {
  23. this.urlPath = urlPath;
  24. this.targetFilePath = targetFilePath;
  25. this.threadNum = threadNum;
  26. downloadThreads = new DownloadThread[threadNum] ;
  27. }
  28. public void downloadFile() throws Exception{
  29. URL url = new URL(urlPath) ;
  30. HttpURLConnection conn = (HttpURLConnection) url.openConnection() ;
  31. conn.setConnectTimeout(4*1000) ;
  32. conn.setRequestMethod("GET") ;
  33. conn.setRequestProperty(
  34. "Accept",
  35. "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +
  36. "application/x-shockwave-flash, application/xaml+xml, " +
  37. "application/vnd.ms-xpsdocument, application/x-ms-xbap, " +
  38. "application/x-ms-application, application/vnd.ms-excel, " +
  39. "application/vnd.ms-powerpoint, application/msword, */*");
  40. conn.setRequestProperty("Accept-Language", "zh-CN");
  41. conn.setRequestProperty("Charset", "UTF-8");
  42. //设置浏览器类型和版本、操作系统,使用语言等信息
  43. conn.setRequestProperty(
  44. "User-Agent",
  45. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; Trident/4.0; " +
  46. ".NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; " +
  47. ".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
  48. //设置为长连接
  49. conn.setRequestProperty("Connection", "Keep-Alive");
  50. //得到要下载文件的大小
  51. fileSize = conn.getContentLength() ;
  52. System.out.println("fileSize:"+fileSize);
  53. //断开连接
  54. conn.disconnect() ;
  55. //计算每条线程需要下载的大小
  56. int preThreadDownloadSize = fileSize/threadNum+1 ;
  57. System.out.println("preThreadDownloadSize:"+preThreadDownloadSize);
  58. RandomAccessFile file = new RandomAccessFile(targetFilePath, "rw") ;
  59. file.setLength(fileSize) ;
  60. file.close() ;
  61. for (int i = 0; i < threadNum; i++) {
  62. // 计算每条线程下载的起始位置
  63. int startPos = i*preThreadDownloadSize+1 ;
  64. RandomAccessFile currentPart = new RandomAccessFile(targetFilePath, "rw") ;
  65. currentPart.seek(startPos) ;
  66. downloadThreads[i] = new DownloadThread(startPos,preThreadDownloadSize,currentPart) ;
  67. new Thread(downloadThreads[i]).start() ;
  68. }
  69. }
  70. /**
  71. * 获取下载的完成百分比
  72. * @return 完成的百分比
  73. */
  74. public double getCompleteRate() {
  75. // 统计多条线程已经下载的总大小
  76. int sumSize = 0;
  77. for (int i = 0; i < threadNum; i++) {
  78. sumSize += downloadThreads[i].hasReadLength;
  79. }
  80. // 返回已经完成的百分比
  81. return sumSize * 1.0 / fileSize;
  82. }
  83. /**
  84. * 用于下载的线程
  85. * @author bing
  86. *
  87. */
  88. private final class DownloadThread implements Runnable{
  89. private int startPos ;
  90. private int preThreadDownloadSize ;
  91. private RandomAccessFile currentPart ;
  92. //已下载长度
  93. private int hasReadLength ;
  94. public DownloadThread(int startPos, int preThreadDownloadSize,
  95. RandomAccessFile currentPart) {
  96. this.startPos = startPos;
  97. this.preThreadDownloadSize = preThreadDownloadSize;
  98. this.currentPart = currentPart;
  99. }
  100. @Override
  101. public void run() {
  102. InputStream inputStream = null ;
  103. try{
  104. URL url = new URL(urlPath) ;
  105. HttpURLConnection conn = (HttpURLConnection) url.openConnection() ;
  106. conn.setConnectTimeout(4*1000) ;
  107. conn.setRequestMethod("GET") ;
  108. conn.setRequestProperty(
  109. "Accept",
  110. "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " +
  111. "application/x-shockwave-flash, application/xaml+xml, " +
  112. "application/vnd.ms-xpsdocument, application/x-ms-xbap, " +
  113. "application/x-ms-application, application/vnd.ms-excel, " +
  114. "application/vnd.ms-powerpoint, application/msword, */*");
  115. conn.setRequestProperty("Accept-Language", "zh-CN");
  116. conn.setRequestProperty("Charset", "UTF-8");
  117. inputStream = conn.getInputStream() ;
  118. inputStream.skip(startPos) ;//定位到开始位置
  119. byte[] buffer = new byte[1024] ;
  120. int temp = 0 ;
  121. while(hasReadLength<preThreadDownloadSize
  122. &&(temp=inputStream.read(buffer))!=-1){
  123. currentPart.write(buffer,0,temp) ;
  124. hasReadLength += temp ;
  125. }
  126. }catch(Exception e){
  127. e.printStackTrace() ;
  128. }finally{
  129. try {
  130. currentPart.close() ;
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. try {
  135. inputStream.close() ;
  136. } catch (Exception e) {
  137. e.printStackTrace();
  138. }
  139. }
  140. }
  141. }
  142. public static void main(String[] args) throws Exception {
  143. String songName = "许嵩 - 半城烟沙.mp3" ;
  144. songName = URLEncoder.encode(songName,"UTF-8") ;
  145. String urlPath = "http://172.16.2.50:8080/mp3/"+songName ;
  146. String targetDir = "E:"+File.separator+songName ;
  147. OmbDownloadOfThreadsUtil odtu = new OmbDownloadOfThreadsUtil(urlPath,targetDir, 6) ;
  148. odtu.downloadFile() ;
  149. }
  150. }

经过以上三步基本上问题已经解决,但如果的文件名含有空格的话还需一步:

URLs是不能包含空格的。URL encoding一般会使用“+”号去替换空格,但后台服务器(我的是Tomcat6.0)又不能把“+”还原为空格,所以导致文件找不到,解决办法:只需把“+”替换为“%20”

    1. public static void main(String[] args) throws Exception {
    2. String songName = "许嵩 - 半城烟沙.mp3" ;
    3. songName = URLEncoder.encode(songName,"UTF-8").replace("+", "%20") ;
    4. String urlPath = "http://172.16.2.50:8080/mp3/"+songName ;
    5. String targetDir = "E:"+File.separator+songName ;
    6. OmbDownloadOfThreadsUtil odtu = new OmbDownloadOfThreadsUtil(urlPath,targetDir, 6) ;
    7. odtu.downloadFile() ;
    8. }