java读取服务器的图片名称_简单的从服务器获取图片保存到本地

packagemyhttp;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;public classHttpUtils {private static String URL_PATH = "http://169.254.174.231:8080/myhttp/1.jpg";publicHttpUtils() {//TODO Auto-generated constructor stub

}public static void saveImageToDisk() throwsIOException{

InputStream inputStream=getInputStream();byte [] data = new byte[1024];int len = 0;

FileOutputStream fileOutputStream= null;try{

fileOutputStream= new FileOutputStream("D:\\1.jpg");while((len = inputStream.read(data))!=-1){

fileOutputStream.write(data,0, len);

}

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}finally{if(inputStream!=null){try{

inputStream.close();

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}if(fileOutputStream != null){try{

fileOutputStream.close();

}catch(Exception e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}public static InputStream getInputStream() throwsIOException{

InputStream inputStream= null;

HttpURLConnection httpURLConnection= null;try{

URL url= newURL(URL_PATH);if(url!=null){

httpURLConnection=(HttpURLConnection) url.openConnection();//设置连接网络的超时时间

httpURLConnection.setConnectTimeout(3000);//如果打算使用 URL 连接进行输入,则将 DoInput 标志设置为 true;如果不打算使用,则设置为 false。默认值为 true。

httpURLConnection.setDoInput(true);

httpURLConnection.setRequestMethod("GET");int responseCode =httpURLConnection.getResponseCode();//服务器状态码,返回200表示服务器成功返回网页

if(responseCode == 200){//从服务器获得一个输入流

inputStream =httpURLConnection.getInputStream();

}

}

}catch(MalformedURLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returninputStream;

}public static void main(String [] args) throwsIOException{//从服务器获得图片保存到D盘

saveImageToDisk();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用常见的HTTP客户端库(如Apache HttpClient或OkHttp)来上传本地图片到云服务器。以下是一些基本的步骤: 1. 创建一个HTTP客户端实例,并设置连接参数。 2. 创建一个HTTP POST请求,并将文件作为请求实体发送。 3. 从响应中读取服务器的响应状态码和消息。 4. 关闭HTTP客户端实例。 以下是一个示例代码,使用Apache HttpClient库上传本地文件到云服务器: ``` import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.File; public class ImageUploader { public static void main(String[] args) throws Exception { // 本地文件路径 String localFilePath = "path/to/local/image.jpg"; // 云服务器URL String serverUrl = "http://server.com/upload"; // 创建HTTP客户端实例 HttpClient httpclient = HttpClients.createDefault(); // 创建HTTP POST请求 HttpPost httppost = new HttpPost(serverUrl); // 创建HTTP请求实体 MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.addBinaryBody("file", new File(localFilePath)); // 设置HTTP请求实体 HttpEntity reqEntity = builder.build(); httppost.setEntity(reqEntity); // 发送HTTP请求,并获取响应 HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); // 从响应中读取状态码和消息 int statusCode = response.getStatusLine().getStatusCode(); String message = EntityUtils.toString(resEntity); // 关闭HTTP客户端实例 httpclient.close(); } } ``` 在上面的示例中,我们使用了Apache HttpClient库的MultipartEntityBuilder类来构建HTTP请求实体,并将本地文件添加到请求中。在发送HTTP请求后,我们从响应中读取了状态码和消息,并关闭了HTTP客户端实例。请注意,这只是一个基本的示例,实际情况可能需要根据具体的云服务器和业务需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值