Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从blackberry中的url下载pdf文件?_Blackberry_Java Me_Download - Fatal编程技术网

从blackberry中的url下载pdf文件?

从blackberry中的url下载pdf文件?,blackberry,java-me,download,Blackberry,Java Me,Download,我正在开发一个blackberry应用程序,其中我从web服务获得了一个pdf url。我需要使用该url下载PDF并将其保存到我的SD卡 请告诉我,有没有可能。很抱歉,我很晚才回复,我在这项任务中获得了成功。我知道这不是一个“非常努力的任务”,但这对我来说是一个教训。我为这个任务使用了以下线程代码 private class DownloadCombiner extends Thread { private String remoteName; private

我正在开发一个blackberry应用程序,其中我从web服务获得了一个pdf url。我需要使用该url下载PDF并将其保存到我的SD卡


请告诉我,有没有可能。

很抱歉,我很晚才回复,我在这项任务中获得了成功。我知道这不是一个“非常努力的任务”,但这对我来说是一个教训。我为这个任务使用了以下线程代码

private class DownloadCombiner extends Thread {
        private String remoteName;
        private String localName;
        private int connection;
        private int chunksize;
        public DownloadCombiner(String remoteName, 
                String localName, int connection, int chunksize) {
            this.remoteName = remoteName;
            this.localName = localName;
            this.connection = connection;
            this.chunksize = chunksize;
        }
        public void run() {
            try {
                int chunkIndex = 0;
                int totalSize = 0;
                /*
                 * File connection
                 */
                 FileConnection file =(FileConnection)Connector.open(localName);
                 if (!file.exists()) {
                     file.create();
                 }
                 file.setWritable(true);
                 OutputStream out = file.openOutputStream();
                 /*
                  * HTTP Connections
                  */
                 String currentFile = remoteName + connectionType();
                 //log("Full URL: " + currentFile);
                 HttpConnection conn;
                 InputStream in;
                 int rangeStart = 0;
                 int rangeEnd = 0;
                 while (true) {
                     //  log("Opening Chunk: " + chunkIndex);
                     conn = (HttpConnection) Connector.open(currentFile, 
                             Connector.READ_WRITE, true);
                     rangeStart = chunkIndex * chunksize;
                     rangeEnd = rangeStart + chunksize - 1;
                     // log("Requesting Range: " + rangeStart + 
                     //      "-" + rangeEnd);
                     conn.setRequestProperty("Range", "bytes=" +
                             rangeStart + "-" + rangeEnd);
                     int responseCode = conn.getResponseCode();
                     if (responseCode != 200 && responseCode != 206)
                     {
                         // log("Response Code = " + conn.getResponseCode());
                         break;
                     }
                     // log("Retreived Range: " + conn.getHeaderField("Content-Range"));
                     in = conn.openInputStream();
                     int length = -1;
                     byte[] readBlock = new byte[256];
                     int fileSize = 0;
                     while ((length = in.read(readBlock)) != -1) {
                         out.write(readBlock, 0, length);
                         fileSize += length;
                         Thread.yield(); // Try not to get cut off
                     }
                     totalSize += fileSize;
                     // log("Chunk Downloaded: " + fileSize + " Bytes");
                     chunkIndex++; // index (range) increase
                     in.close();
                     conn.close();
                     in = null;
                     conn = null;
                     /*
                      * Pause to allow connections to close and other Threads
                      * to run.
                      */
                     Thread.sleep(1000);
                 }
                 // log("Full file downloaded: " + totalSize + " Bytes");
                 out.close();
                 file.close();
                 // log("Wrote file to local storage");
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }
        private String connectionType() {
            switch (connection) {
            case 1:
                return ";deviceside=false";
            case 2:
                return ";deviceside=true;interface=wifi";
            default:
                return ";deviceside=true";
            }
        }
    }

您可以下载一个字节数组中的PDF文件,然后将这些字节写入扩展名为
.PDF
的文件中。对于suggestion Rupak,我也在考虑这个问题。我会试着尽快告诉你结果。