能不能用JAVA编写一个程序从网上下载一张图片呢?求完整程序!
答案:2 悬赏:10
解决时间 2021-04-07 18:48
- 提问者网友:回憶丶初
- 2021-04-07 14:45
能不能用JAVA编写一个程序从网上下载一张图片呢?求完整程序!
最佳答案
- 二级知识专家网友:24K纯糖
- 2021-04-07 15:49
package com.capinfotech.net;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageRequest {
public static void main(String[] args) throws IOException {
URL url = new URL("https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/album/w%3D2048/sign=1bb39918cdbf6c81f7372be88806b035/9345d688d43f879423f3355ed31b0ef41bd53ab5.jpg");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream inputStream = conn.getInputStream(); //通过输入流获得图片数据
byte[] getData = readInputStream(inputStream); //获得图片的二进制数据
File imageFile = new File("tupian.jpg");
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(getData);
fos.close();
System.out.println(" read picture success");
}
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImageRequest {
public static void main(String[] args) throws IOException {
URL url = new URL("https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/album/w%3D2048/sign=1bb39918cdbf6c81f7372be88806b035/9345d688d43f879423f3355ed31b0ef41bd53ab5.jpg");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream inputStream = conn.getInputStream(); //通过输入流获得图片数据
byte[] getData = readInputStream(inputStream); //获得图片的二进制数据
File imageFile = new File("tupian.jpg");
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(getData);
fos.close();
System.out.println(" read picture success");
}
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
}
全部回答
- 1楼网友:兮沫♡晨曦
- 2021-04-07 16:49
public class person {
string name;
string sex;
int age;
public person(){
}
public person(string name,string sex,int age){
this.name = name;
this.age = age;
this.sex =sex;
}
public void tostring(person person){
string age = integer.tohexstring(person.age);
string name =person.name;
string sex = person.sex;
system.out.println("姓名:"+name);
system.out.println("性别:"+sex);
system.out.println("年龄:"+age);
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string getsex() {
return sex;
}
public void setsex(string sex) {
this.sex = sex;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
}
public class creatperson {
public static void main(string[] args){
person new1 = new person("张三","男",22);//调用person
new person().tostring(new1);//转换为string并输出
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |