开发者社区> 问答> 正文

OSS无法下载到本地?为什么


Exception in thread "main" java.io.FileNotFoundException: C:\test (拒绝访问。)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:212)
    at java.io.RandomAccessFile.<init>(RandomAccessFile.java:98)
    at com.BlockDownloadSample.main(BlockDownloadSample.java:39)

展开
收起
猫猫虫 2015-01-11 21:43:12 7958 0
3 条回答
写回答
取消 提交回答
  • LT是个伪程序员
    程序没有写文件到C:\Test权限,试试把Test的文件属性-〉安全里访问权限改改。

    -------------------------

    楼主那个是分块下载大文件的代码?没有试过上述代码。一般对于下载来说,只需要提供一个URL即可,如果要分块的话,现在的下载工具都会分块下载。你可以先试试下载一个小文件看是否OK。
       参考代码:

    public void downloadNet(HttpServletResponse response) throws MalformedURLException {
            // 下载网络文件
            int bytesum = 0;
            int byteread = 0;

            URL url = new URL("http://aliyunbbs.oss.aliyuncs.com/attachment/upload/middle/84/1480587189050384.png");

            try {
                URLConnection conn = url.openConnection();
                InputStream inStream = conn.getInputStream();
                FileOutputStream fs = new FileOutputStream("c:/abc.png");

                byte[] buffer = new byte[1204];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }





    2015-01-12 15:59:35
    赞同 展开评论 打赏
  • ReOSS无法下载到本地?为什么
    有代码分享一下么 这个看不出原因的
    2015-01-12 15:52:52
    赞同 展开评论 打赏
  • ReOSS无法下载到本地?为什么
    各位   这是什么原因啊?是没有权限访问C:\test吗?

    -------------------------

    ReOSS无法下载到本地?为什么这是javaSDK例子的代码拷贝下来的
        public static void main(String[] args) throws IOException,
                InterruptedException {
            final String localFilePath = "C:\\test";
            final String bucketName = "user-1";
            final String fileKey = "public-bg9";

            ObjectMetadata objectMetadata = client.getObjectMetadata(bucketName,
                    fileKey);
            long partSize = 1024 * 1024 * 5;
            long fileLength = objectMetadata.getContentLength();
            //随机流可以读也可以写,rw代表“可读写”
            RandomAccessFile file = new RandomAccessFile(localFilePath, "rw");
            file.setLength(fileLength);
            file.close();

            int partCount = calPartCount(fileLength, partSize);
            System.out.println("需要下载的文件分块数:" + partCount);
            executorService = Executors.newFixedThreadPool(5);
            List<String> eTags = Collections
                    .synchronizedList(new ArrayList<String>());

            for (int i = 0; i < partCount; i++) {
                final long startPos = partSize * i;
                final long endPos = partSize
                        * i
                        + (partSize < (fileLength - startPos) ? partSize
                                : (fileLength - startPos)) - 1;

                executorService.execute(new BlockDownloadThread(startPos, endPos,
                        localFilePath, bucketName, fileKey, eTags));
            }
            executorService.shutdown();
            while (!executorService.isTerminated()) {
                executorService.awaitTermination(5, TimeUnit.SECONDS);
            }

            if (eTags.size() != partCount) {
                throw new IllegalStateException("下载失败,有Part未下载成功。");
            }
        }

    -------------------------

    回3楼千鸟的帖子
    权限应该没问题吧   你看看
    2015-01-11 21:44:29
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载