2012-03-27 5 views
0

いくつかの制限事項もあります。 任意のライブラリ/技術を自由に使用できますただし、java.net.Url、java.net.URIまたはjava.net.UrlConnectionは例外です。これらのクラスを使用するソリューションは受け付けられません。エラー処理と可読性を向上させるために、クラス署名を自由に変更することができます。HTTPプロトコルを使用してURLのリソースを取得し、ブラウザに表示された内容をファイルに保存するJavaクラスを作成します。

クラス概要:私はインタビューで、以前の今日、この質問をした

public JGet extends Object { 

public JGet(String urlToPage, String saveToFilename){ 

} 

    public Object getContents(){ 

    } 

} 

。 Javaは私の最強の言語ではありませんが、私はそれをベストショットにしました。 .javaファイルを提出した後、雇用マネージャーは次のようなコメントで答えました: "あなたは手元の問題を解決していないし、エラーチェックもなく、呼び出しクラスとの通信もなく、例外もマスクされていない。私のコードはどのように問題を解決しないのですか?私は自分の過ちから学び、改善したい。私のコードは以下の通りです。

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import org.apache.commons.io.IOUtils; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.*; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 


public class JGet extends Object 
{ 

String URL; 

String filename; 

public JGet(String URL_with_content, String filename_of_content) 
{ 
    URL=URL_with_content; 
    filename=filename_of_content; 

} 

//This method gets the html content of a JGet object's URL and stores it in the file located at 'filename' 
//In order to accomplish this, apache's http components library was used: http://hc.apache.org/ 
public Object getContents() 
{ 
    HttpClient client=new DefaultHttpClient(); 
    HttpGet httpget=new HttpGet(URL); 

    try 
    { 
     HttpResponse response=client.execute(httpget); 

     //getting the content of the url 
     InputStream input=response.getEntity().getContent(); 

     OutputStream output = new FileOutputStream(filename); 

     //taking inputstream and writing it to file using Apache's IOUtils library: http://commons.apache.org/io/ 
     IOUtils.copy(input,output); 
    } 

    catch (ClientProtocolException e) 
    { 
     System.err.println("ClientProtocolException with "+e.getMessage()); 
     e.printStackTrace(); 
    } 

    catch (IOException e) 
    { 
     System.err.println("IOException with "+e.getMessage()); 
     e.printStackTrace(); 
    } 

    return null; 

} 

} 
+0

あなたはJDKクラスのみを使用することになっていましたか、そうしたときにサードパーティライブラリを使用できましたか?さもなければ、これはあなたのソリューションを失敗させる良い理由になる可能性があります。おそらく彼らはJDKクラスのみを使用して解決策を期待していました。 –

+0

@edalorzo "あなたは任意のライブラリを自由に使用することができます..." –

+2

これはhttp://codereview.stackexhange.comに適していますが、 'getContents'が' Object'を返すだけでなく、常に*ヌルを返します。 –

答えて

0

うーん、私は文の私の解釈にいくつかのコメントを行います。おそらく他の人が異なる側面についてコメントすることができます。

は、あなたのコードをファイルに指定したURLの内容をコピーしないことが表示されます

「あなたは手で問題を解決しませんでした」。それは最良の実装ではないかもしれませんが、問題を解決します。

[...]エラーチェックが[...]ありませんでした

これは多くのことを意味するかもしれません:

  • パラメータがで渡された場合は、チェックしている可能性が呼び出し側のクラスはnullではありませんでした。
  • URL文字列が実際にHTTP URLに対応していることを確認できました。
  • 提供されたファイルパスが存在するかどうかを確認できました。やや

    private final URI urlToPage; 
    private final String saveToFileName; 
    
    public JGet(String urlToPage, String saveToFilename) { 
    
        if(urlToPage == null){ 
         throw new NullPointerException("The URL must not be null"); 
        } 
    
        if(saveToFilename == null){ 
         throw new NullPointerException("The name of the destination file must not be null"); 
        } 
    
        //some other questions we could ask 
        //does this path exist? (we may let the stream creation fails later also 
        //does the file exist? (ditto) 
        //can we overwrite files (in this case we may stop it here) 
        this.saveToFileName = saveToFilename; 
    
        try { 
          //is this actually a http URL? 
          //we may check protocol 
         this.urlToPage = new URI(urlToPage); 
        } catch (URISyntaxException e) { 
         throw new IllegalArgumentException("Invalid URL provided: "+ urlToPage); 
        } 
    } 
    

    のようなおそらく

呼び出し元のクラスと例外はなし通信

をマスクしたまあ、私はここでの問題は、あなたの実装では、呼び出し元のクラスが持つことだと思いますプロセスが正常に終了したか、実際に失敗したかを判断する方法はありません。戻り値オブジェクトを使用して成功または失敗を示すか、またはメソッドをvoidに変更して、何らかの問題が発生した場合に呼び出し元に例外を再発行することができます。

あなたの実装では、destinyファイルを開いたときにそれが空であれば、プロセスは失敗するか、URLに何も含まれていないためですか?

public void getContents() throws IOException { 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpGet httpGet = new HttpGet(this.urlToPage); 
    HttpResponse response = httpClient.execute(httpGet); 
    HttpEntity entity = response.getEntity(); 
    IOUtils.copy(entity.getContent(), new FileOutputStream(this.saveToFileName)); 
} 

それとも専門の確認/チェックされない例外へのIOExceptionをラップし、その1を投げる:おそらく多少このような

0

例外処理については、私はあなたが例外を再スローして、呼び出し元のコードは、それをどうするか決めさせなければならないと思う:

public JGet { //extends Object { not necessary 

    public JGet(String urlToPage, String saveToFilename){ 

    } 

    public boolean getContents() throws IOExceprion { 
     //returns true if successful. 
     //use try/catch to rethrow ClientProtocolException as IOException 
    } 

}