java下载文件未能加载资源_Java Web start – 无法加载资源

当我浏览服务器的IP地址时,我有一个用

java web start加载的jar.

一旦我尝试服务器名称,我得到以下异常:

com.sun.deploy.net.FailedDownloadException: Unable to load resource:

at com.sun.deploy.net.DownloadEngine.actionDownload(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getResourceCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getResourceCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getResource(Unknown Source)

at com.sun.javaws.LaunchDownload$DownloadTask.call(Unknown Source)

at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

包装异常:

java.io.FileNotFoundException:

at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

at com.sun.deploy.net.BasicHttpRequest.doRequest(Unknown Source)

at com.sun.deploy.net.BasicHttpRequest.doRequest(Unknown Source)

at com.sun.deploy.net.BasicHttpRequest.doGetRequest(Unknown Source)

at com.sun.deploy.net.DownloadEngine.actionDownload(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getResourceCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getResourceCacheEntry(Unknown Source)

at com.sun.deploy.net.DownloadEngine.getResource(Unknown Source)

at com.sun.javaws.LaunchDownload$DownloadTask.call(Unknown Source)

at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

Caused by: java.io.FileNotFoundException:

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

at java.net.HttpURLConnection.getResponseCode(Unknown Source)

... 15 more

JNLP:

spec="1.0+"

codebase="http://servername/Site/Views/.."

href="">

title

vendor

description

short

CD6B172DEC34924D83EA64A61A6550BE1271325929392

http://servername/file.asp

/Site

知道我应该找什么?

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Web 文件下载时,如果文件较大,一次性下载可能会耗费很长时间,而且如果下载过程中发生网络中断或者其他问题,就需要重新下载。为了提高用户体验,我们可以实现文件下载的断点续传功能。 断点续传的原理是:客户端发起下载请求时,服务端会将文件分成若干个片段,并为每个片段设置一个唯一的标识符,客户端下载时只需指定要下载的片段标识符即可。如果下载中断,客户端再次发起请求时,只需告诉服务端已经下载了哪些片段,服务端会根据这些片段的标识符返回还未下载的片段。 下面是一个简单的 Java Web 文件断点续传的示例代码: ```java @RequestMapping("/download") public void download(HttpServletRequest request, HttpServletResponse response) throws IOException { // 文件路径 String filePath = "D:\\test\\test.zip"; File file = new File(filePath); if (!file.exists()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // 文件名 String fileName = file.getName(); // 文件长度 long fileLength = file.length(); // 下载起始位置 long start = 0; // 下载结束位置 long end = fileLength - 1; // 获取 Range 头 String rangeHeader = request.getHeader("Range"); if (rangeHeader != null) { // 根据 Range 头解析起始位置和结束位置 String[] range = rangeHeader.substring("bytes=".length()).split("-"); start = Long.parseLong(range[0]); if (range.length > 1 && !"".equals(range[1])) { end = Long.parseLong(range[1]); } } // 设置 Content-Length, Content-Range 和 Content-Type 头 response.setHeader("Content-Length", String.valueOf(end - start + 1)); response.setHeader("Content-Range", "bytes " + start + "-" + end + "/" + fileLength); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName); // 获取文件输入流 RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r"); randomAccessFile.seek(start); InputStream inputStream = new BufferedInputStream(new FileInputStream(randomAccessFile.getFD())); // 获取输出流 ServletOutputStream outputStream = response.getOutputStream(); // 缓冲区大小 byte[] buffer = new byte[1024]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } // 关闭流 inputStream.close(); outputStream.close(); } ``` 在代码中,我们首先获取文件路径和文件名,然后根据 Range 头中的信息计算出要下载的片段的起始位置和结束位置。接着,设置 Content-Length, Content-Range 和 Content-Type 头,并获取文件输入流和输出流,将要下载的片段写入输出流中,最后关闭流。 需要注意的是,代码中使用了 RandomAccessFile 类来实现文件指针的移动,从而实现文件的随机读取。另外,由于使用了 RandomAccessFile 类,我们需要通过调用 FileInputStream 的 getFD() 方法获取文件描述符,否则会导致文件的随机读取失效。 上述代码只是一个示例,实际应用中还需要考虑很多细节问题,比如请求头的验证、下载速度的限制等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值