我想在我的Java代码中使用WiFi连接.如何使用特定的WiFi网络接口从Internet下载文件?
换句话说,我如何强制java通过特定的网络接口发送数据包?
我使用以下代码使用socket而不是url.openconnection()从指定的url下载文件; 下载后我检查它不工作...当我用编辑器打开文件时它是完全空白的文件内没有数据(空文件)需要建议??? ... ...
try {
String address="http://tineye.com/images/widgets/mona.jpg";
URL url_of_file=new URL(addres);
String hostaddress=url_of_file.getHost();
Socket mysocket=new Socket(hostaddress, 80);
System.out.println("Socket opened to " + hostaddress + "\n");
String file=url_of_file.getFile();
System.out.println(" file = "+file);
OutputStreamWriter osw=new OutputStreamWriter(mysocket.getOutputStream());
osw.write("GET " + file + " HTTP/1.0\r\n\n");
osw.flush();
dis = new DataInputStream(mysocket.getInputStream());
fileData = new byte[7850];
for (int x = 0; fileData[x] > 0; x++){
fileData[x] = (byte) dis.read();
}
// close the data input stream
fos = new FileOutputStream(new File("C:\\Users\\down-to\\filedownloaded.jgp")); //create an object representing the …Run Code Online (Sandbox Code Playgroud)