按照文件路径和文件名创建文件夹及下载文件的一些总结

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import com.utstar.sobeyadapter.domain.Constant;


@Service
public class DownloadPictureService {
Logger logger = LoggerFactory.getLogger(DownloadPictureService.class);


/**
* 将图片写入服务器

* @param picture
* @param fileName
*/
public void writePictureToServer(byte[] picture, String jsondataPath, String fileName) {
try {
File file = new File(jsondataPath);
if (!file.exists()) {
file.mkdirs();
logger.info("创建路径成功,路径为:"+jsondataPath);
}
File file2 = new File(jsondataPath, fileName);
if (!file2.exists()) {
try {
file2.createNewFile();
logger.info("创建文件成功,文件名为:"+fileName);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
String fileEntity = jsondataPath + "/" + fileName;
// String fileEntity = jsondataPath + "//" + fileName;
/*
* FileOutputStream writerStream = new FileOutputStream(fileEntity);
* BufferedWriter writer = new BufferedWriter(new
* OutputStreamWriter(writerStream, "UTF-8")); // BufferedWriter
* writer=new BufferedWriter(new FileWriter(fileEntity));
* writer.write(picture); writer.close();
*/
logger.info("图片下载到的路径为:"+fileEntity);
FileOutputStream fileOutputStream = new FileOutputStream(fileEntity);
fileOutputStream.write(picture);
fileOutputStream.flush();
fileOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}


public byte[] getPictureByUrl(String strUrl) {
try {
logger.info("图片URL是:"+strUrl);
logger.info("-------------------------------开始获取图片数据流-------------------------------");
URL url = new URL(strUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(Constant.DownloadPictureTimeout * 1000);//设置链接超时时间,单位毫秒
InputStream inputStream = connection.getInputStream();
byte[] bsPicture = readInputStream(inputStream);
if(bsPicture!=null&&bsPicture.length>0){
logger.info("-------------------------------成功获取图片数据流-------------------------------");
}
return bsPicture;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}


public byte[] readInputStream(InputStream inStream) {
try {
logger.info("--------进入读取状态---------");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
inStream.close();
outputStream.close();
return outputStream.toByteArray();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值