android开发--文件下载在手机报错
package cc.download;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button = null;
// private Button button2 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
DownloadFile file = new DownloadFile();
file.down("http://192.168.1.100:8080/examples/cc.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package cc.download;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Environment;
import android.util.Log;
public class DownloadFile {
String path = Environment.getExternalStorageDirectory()+"/";
public boolean isEx()
{
return false;
}
public File down(String str) throws Exception
{
File file = new File(path+"sds.txt");
OutputStream ot = null;
ot = new FileOutputStream(file);
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
InputStream is = connection.getInputStream();
byte bt[] = new byte[1024 * 4];
while(is.read(bt)!=-1)
{
ot.write(bt);
}
ot.flush();
ot.close();
return file;
}
public File createDir()
{
File file = null;
return file;
}
}
http://d.hiphotos.baidu.com/zhidao/pic/item/8b82b9014a90f603d026c9463b12b31bb051ed19.jpg
上面两段是java代码下面是报的错误的链接 图片我市在市传不上来,在电脑虚拟机上正常但是在手机上就会报错 为什么???在手机上下下来的文件是空的 --------------------编程问答-------------------- 下载地址是局域网的,那手机需要连接局域网WIFI,获取到 192.168.1.xxx 这样的 IP 。
补充:移动开发 , Android