使用 java 编程,获取网路上的pdf文件
我想编写个程序,知道这个pdf文件的下载的链接地址,直接使用程序将其下载,保存在本地;如果有别的自动下载的方式也可以啊...
我想编写个程序,知道这个pdf文件的下载的链接地址,直接使用程序将其下载,保存在本地;如果有别的自动下载的方式也可以啊
展开
1个回答
展开全部
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
public class MyService2 extends Service {
Thread thread = new Thread() {
@Override
public void run() {
try {
// 声明抛出所有例外
URL tirc = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");
// 构建一URL对象
InputStream is = tirc.openStream();
FileOutputStream fos = new FileOutputStream(
"/mnt/sdcard/baidu_sylogo21.gif");
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while (len != -1) {
fos.write(buffer, 0, len);
len = is.read(buffer);
}
fos.close();
is.close();
handler.sendEmptyMessage(1);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(MyService2.this, "下载完成...", Toast.LENGTH_LONG)
.show();
break;
}
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service onStart...", Toast.LENGTH_LONG).show();
thread.start();
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
}
把链接地址改了就可以了
import java.io.InputStream;
import java.net.URL;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
public class MyService2 extends Service {
Thread thread = new Thread() {
@Override
public void run() {
try {
// 声明抛出所有例外
URL tirc = new URL("http://www.baidu.com/img/baidu_sylogo1.gif");
// 构建一URL对象
InputStream is = tirc.openStream();
FileOutputStream fos = new FileOutputStream(
"/mnt/sdcard/baidu_sylogo21.gif");
byte[] buffer = new byte[1024];
int len = is.read(buffer);
while (len != -1) {
fos.write(buffer, 0, len);
len = is.read(buffer);
}
fos.close();
is.close();
handler.sendEmptyMessage(1);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
Toast.makeText(MyService2.this, "下载完成...", Toast.LENGTH_LONG)
.show();
break;
}
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service onStart...", Toast.LENGTH_LONG).show();
thread.start();
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
}
把链接地址改了就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询