Java下载图片服务器上的资源

编程入门 行业动态 更新时间:2024-10-07 22:21:23

Java<a href=https://www.elefans.com/category/jswz/34/1764454.html style=下载图片服务器上的资源"/>

Java下载图片服务器上的资源

工具类代码如下:

package util;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.MalformedURLException;
import java.URL;
import java.URLConnection;import org.apache.log4j.Logger;/*** 下载http对应URL的图片资源**/
public class DownloadImage{/*** 日志*/protected  final Logger logger = Logger.getLogger(DownloadImage.class);private String ImageRootPath = "/data/application/tempImg";/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {DownloadImage downloadImage = new DownloadImage();String httpUrlStr = ".jpg?1523447843";String fileName  = System.currentTimeMillis() + ".png";downloadImage.download(httpUrlStr, fileName);}/*** 下载图片* @param httpUrlStr* @param subFolderName* @param filename* @return*/public String download(String httpUrlStr, String filename) {return download(httpUrlStr,filename,ImageRootPath);}@SuppressWarnings("finally")private String download(String httpUrlStr, String filename,String savePath){InputStream is = null;OutputStream os = null;String filePath = null;try {filePath = savePath + File.separator + filename;logger.info(String.format("下载图片... url = {%s}, filePath = {%s} ", httpUrlStr, filePath));// 构造URLURL url = new URL(httpUrlStr);// 打开连接URLConnection con = url.openConnection();//设置请求超时为5scon.setConnectTimeout(5*1000);// 输入流is = con.getInputStream();// 1K的数据缓冲byte[] bs = new byte[1024];// 读取到的数据长度int len;// 输出的文件流File sf=new File(savePath);if(!sf.exists()){sf.mkdirs();}os = new FileOutputStream(filePath);// 开始读取while ((len = is.read(bs)) != -1) {os.write(bs, 0, len);}logger.info(String.format("下载图片成功 图片存放路径 = {%s}", filePath));} catch (MalformedURLException e) {logger.error("下载图片失败 : " + e.getMessage());e.printStackTrace();} catch (Exception e){logger.error("普通异常下载图片失败 : " + e.getMessage());} finally {// 完毕,关闭所有链接try {if(null != os){os.close();}if(null != is){is.close();}} catch (IOException e) {logger.error("下载图片关闭流失败: " + e.getMessage());}return filePath;}}/*** 删除临时文件  注意,只能删除指定目录下面的临时文件* @param   supplierId  商户ID* @param   fileName    被删除文件的文件名* @return 单个文件删除成功返回true,否则返回false*/public boolean deleteFile(String fileName) {boolean flag = false;try{String sPath = getImageRootPath() + File.separator + fileName;logger.info(String.format("删除临时文件 path = {%s} ", sPath));File file = new File(sPath);// 路径为文件且不为空则进行删除if (file.isFile() && file.exists()) {file.delete();flag = true;}} catch(Exception e){logger.info("删除文件异常 " + e.getMessage());}return flag;}public String getImageRootPath() {return ImageRootPath;}public void setImageRootPath(String ImageRootPath) {this.ImageRootPath = ImageRootPath;} 
}

运行结果如下:



下载结果:



更多推荐

Java下载图片服务器上的资源

本文发布于:2024-02-27 19:57:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1766101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:下载图片   器上   资源   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!