按照文件路径和文件名创建文件夹及下载文件的一些总结
来源:互联网 发布:淘宝刷好评多少钱一个 编辑:程序博客网 时间:2024/06/07 07:46
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;
}
}
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
- 按照文件路径和文件名创建文件夹及下载文件的一些总结
- C++获得文件夹下所有文件的路径及文件名
- C#获取文件名 文件路径 文件夹路径
- Java创建文件夹和JSP下载文件
- 按照文件名的数字大小排序文件
- windows下创建没有文件名的文件或者文件夹
- java文件操作 之 创建文件夹路径和新文件
- Java-读取某个目录下所有文件、文件夹和3种从文件路径中获取文件名的方法
- 多系统,多种浏览器下:文件上下载的文件名和路径问题
- ubuntu创建及删除文件和文件夹
- 判断和创建文件及文件夹
- 在磁盘指定路径,创建文件夹及文件
- 创建文件路径及文件夹(老师代码)
- 170607 python创建文件夹及扫描路径文件
- Python文件及文件夹的一些操作
- 文件夹及文件的一些操作
- 盘符路径+文件夹路径+文件名+文件后缀
- C#根据文件路径获取该文件夹路径、文件名、后缀名
- api url
- bzoj4917: Hash Killer IV
- 高并发网络连接数因端口数量受限问题
- Android零基础入门第31节:几乎不用但要了解的AbsoluteLayout绝对布局
- java 集合 容器 List Set Map区别
- 按照文件路径和文件名创建文件夹及下载文件的一些总结
- WPF 实现新手指引功能 DEMO
- 玩转 NSString
- JS学习-generator
- SVN版本控制学习
- 在润乾中可以给扩展单元格中的最大值设置背景色
- MySQL工具类的封装
- 51nod 与7无关的数
- JVM内存溢出,在myeclipse中加大tomcat的jvm内存java.lang.outofmemoryerror.permgenspace