1

我实现了从 HTML 内容中读取 txt 文件的 Java 网络程序。我能够使用 HTML_OK 方案,但是当我尝试获取“部分 GET”请求时,连接再次返回“HTML_OK”。我不知道为什么会发生这种情况,我搜索了互联网,但找不到任何答案。我写的代码是:

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class FileDownloader {


    public static void main(String[] args){

        try{

            int bufSize = 8 * 1024;
            URL url = null;
            BufferedInputStream fromURL = null;
            BufferedOutputStream toFile = null;

            /*
            if(args[1].charAt(0) == 'h' && args[1].charAt(1) == 't' &&
                    args[1].charAt(2) == 't' && args[1].charAt(2) == 'p'){
                url = new URL(args[1]); 
            }
            else{
                url = new URL("http://" + args[1]);
            }
            * 
            */
            // silinecek
            url = new URL("http://www.cs.bilkent.edu.tr/~morhan/cs421/file_2.txt"); 

            // Conncecting the URL to HttpURLConnection
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            // Setting up the outputfileName
            String outputfileName = url.getPath().substring(url.getPath().lastIndexOf("/") + 1);
            File outputFile = new File(outputfileName);

            // Scenario - 1 ( 200 OK Message From HTML )
            if(args.length == 3){ // 3 OLACAK
                con.setRequestMethod("GET");
                System.out.println("Size of the file is: " + con.getContentLength());
                fromURL = new BufferedInputStream(con.getInputStream(), bufSize);
                toFile = new BufferedOutputStream(new FileOutputStream(outputFile), bufSize);   

                if(con.getResponseCode() == HttpURLConnection.HTTP_OK){
                    // READING BYTE BY BYTE HERE
                    int read = -1;
                    byte[] buf = new byte[bufSize];
                    while ((read = fromURL.read(buf, 0, bufSize)) >= 0) {
                        toFile.write(buf, 0, read);
                    }
                    toFile.close();
                    System.out.println("ok");
                }
            // Scenario - 2 (206 Partial Get Message From HTML
            }else if(args.length == 0){ // 5 OLACAK
                con.setRequestMethod("HEAD");

                if(con.getResponseCode() == HttpURLConnection.HTTP_OK){

                    System.out.println("Size of the file is: " + con.getContentLength());

                    //byte startRange =  0; //Byte.parseByte(args[3]);
                    //byte finishRange =  24;//Byte.parseByte(args[4]);

                    if(startRange < 0 || finishRange > ((byte)con.getContentLength()) - 1
                            || startRange > finishRange){
                        System.out.println("Range is not OK.");
                    }else{                     


                        con.setRequestMethod("GET");

                        // I am Setting the range here, however the program 
                        // always returns 200 OK message instead of a 206 one
                        con.setRequestProperty("Range: ", "bytes=0-20"); 

                        System.out.println(con.getRequestMethod());

                        fromURL = new BufferedInputStream(con.getInputStream(), bufSize);
                        toFile = new BufferedOutputStream(new FileOutputStream(outputFile), bufSize);

                        System.out.println(con.getResponseCode());

                        if(con.getResponseCode() == HttpURLConnection.HTTP_PARTIAL){
                            // NOT DOING THE IF STATEMENT
                            System.out.println("aaaa");
                        }

                        System.out.println("bbbb");
                    }
                }
            }else{
                System.out.println("Wrong argument count.");
            }

        }catch(MalformedURLException mue){
            mue.printStackTrace();
        }catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

谁能帮我解决这个问题?

4

0 回答 0