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中,进行图片上传时,需要指定服务器上的保存路径来存储上传的图片。一种常见的设置保存路径的方法是通过配置文件来进行配置。具体步骤如下所示: 首先,在配置文件中设置保存路径的属性,可以命名为"upload.path",该属性的值就是保存图片的路径。例如,可以设置为"upload.path=D:/uploads/"。 然后,在Java代码中读取配置文件中的属性值,并使用该值作为保存路径。可以使用Properties类来读取配置文件的内容。例如,可以使用以下代码来读取保存路径: ```java Properties properties = new Properties(); try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("config.properties")) { properties.load(inputStream); } catch (IOException e) { e.printStackTrace(); } String savePath = properties.getProperty("upload.path"); ``` 最后,可以通过以下代码来保存上传的图片到指定的路径: ```java Part filePart = request.getPart("file"); // 获取上传的文件 String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // 获取文件名 String filePath = savePath + fileName; // 拼接保存路径和文件名 try (InputStream inputStream = filePart.getInputStream(); FileOutputStream outputStream = new FileOutputStream(filePath)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } } ``` 上述代码中,通过request.getPart("file")获取上传的文件,并通过getSubmittedFileName()方法获取文件名。然后,通过FileOutputStream将文件保存到指定的路径。 以上就是使用Java进行图片上传时,设置服务器保存路径的方法。根据实际需求,可以灵活地配置保存路径,并通过代码将上传的图片保存到指定的路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值