赞
踩
1.只能将heic格式文件读取成二进制流才能成功---用imageIO包读取返回结果为null
- /**
- * 将二进制转换成图片保存---------场景(将heic格式转化为jpg格式)
- * @param imgStr 二进制流转换的字符串
- * @param imgPath 图片的保存路径
- * @param imgName 图片的名称
- * @return true:保存正常 false:保存失败
- */
- // @ClearInterceptor(ClearLayer.ALL)
- public boolean saveToImgByBytes(File imgFile, String imgPath, String imgName){
- boolean stateInt = true;
- if(imgFile.length() > 0){
- try {
- File file=new File(imgPath, imgName);//可以是任何图片格式.jpg,.png等
- FileOutputStream fos=new FileOutputStream(file);
- FileInputStream fis = new FileInputStream(imgFile);
- byte[] b = new byte[1024];
- int nRead = 0;
- while ((nRead = fis.read(b)) != -1) {
- fos.write(b, 0, nRead);
- }
- fos.flush();
- fos.close();
- fis.close();
- } catch (Exception e) {
- stateInt = false;
- e.printStackTrace();
- } finally {
-
- }
- }
- return stateInt;
- }

2.下载的时候直接判断是否是heic格式,如果是,直接转码,写成jpg格式保存
- // 参数filename----写上后缀就能直接转码成jpg格式
-
- /**
- * @从制定URL下载文件并保存到指定目录
- * @param filePath 文件将要保存的目录
- * @param method 请求方法,包括POST和GET
- * @param url 请求的路径
- * @return
- */
- public String downloadFromUrl(String url, String filePath, String method, String fileName){
-
- // fileName = 全景图的name-枚举 + 序号
-
- //创建不同的文件夹目录
- File file = new File(filePath);
- //判断文件夹是否存在
- if (!file.exists()) {
- //如果文件夹不存在,则创建新的的文件夹
- file.mkdirs();
- }
- FileOutputStream fileOut = null;
- HttpURLConnection conn = null;
- InputStream inputStream = null;
- String contentLength = null;
- try {
- // 建立链接
- URL httpUrl=new URL(url);
- conn = (HttpURLConnection) httpUrl.openConnection();
- //以Post方式提交表单,默认get方式
- conn.setRequestMethod(method);
- conn.setDoInput(true);
- conn.setDoOutput(true);
- // post方式不能使用缓存
- conn.setUseCaches(false);
- //连接指定的资源
- conn.connect();
- //获取网络输入流
- inputStream=conn.getInputStream();
- BufferedInputStream bis = new BufferedInputStream(inputStream);
- //判断文件的保存路径后面是否以/结尾
- if (!filePath.endsWith("/")) {
- filePath += "/";
- }
- //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
- fileOut = new FileOutputStream(filePath + fileName);
- BufferedOutputStream bos = new BufferedOutputStream(fileOut);
-
- byte[] buf = new byte[4096];
- int length = bis.read(buf);
- //保存文件
- while(length != -1) {
- bos.write(buf, 0, length);
- length = bis.read(buf);
- }
- // 获取response header值
- contentLength = getContentLength(conn);
- bos.close();
- bis.close();
- conn.disconnect();
- } catch (Exception e) {
- LOGGER.error("下载文件异常:{}", e);
- }
-
- return contentLength;
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。