いくつかの制限事項もあります。 任意のライブラリ/技術を自由に使用できますただし、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;
}
}
あなたはJDKクラスのみを使用することになっていましたか、そうしたときにサードパーティライブラリを使用できましたか?さもなければ、これはあなたのソリューションを失敗させる良い理由になる可能性があります。おそらく彼らはJDKクラスのみを使用して解決策を期待していました。 –
@edalorzo "あなたは任意のライブラリを自由に使用することができます..." –
これはhttp://codereview.stackexhange.comに適していますが、 'getContents'が' Object'を返すだけでなく、常に*ヌルを返します。 –