同样是feign调用,返回结果不同。第一个是ResultEntity, 第二个是InputStream输入流。
下面是第一个feign请求的controller的返回ResultEntity:
下面是第二个feign请求的controller的返回void:
serviceImpl方法将file文件类型转化为字节数组,并返回字节数组:
byte[] filebyte = Filebyte(file);
return filebyte;
公共方法:
public static byte[] Filebyte(File file){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
long fileSize = file.length();
log.info("File size is " + fileSize + " bytes");
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
另外一个功能:将多个文件打成zip包,代码如下:
@Slf4j
public class FileToZip {
private FileToZip(){}
/**
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
* @param zipFilePath :压缩后存放路径
* @param fileName :压缩后文件的名称
* @return
*/
public static byte[] fileToZip(List<ZipDto> fileList, String zipFilePath, String fileName){
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
File zipPath = new File(zipFilePath);
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
//判断源文件是否存在
try {
if (!zipPath.exists()) {
zipPath.mkdirs();
}
//判断压缩后文件是否会重复
if(zipFile.exists()){
log.info(zipFilePath + "目录下已存在名字为" + fileName +".zip" +"文件");
}else{
log.info(zipFilePath + "目录下不存在名字为" + fileName +".zip" +"文件,开始创建文件。");
zipFile.createNewFile();
//获取源文件夹下的所有文件
if(CollectionUtils.isEmpty(fileList)){
log.info("待压缩的文件目录:" + fileList + "里面不存在文件,无需压缩.");
}else{
log.info(zipFilePath + "目录下已存在名字为" + fileName +".zip" +"文件,开始读取文件,写入压缩包。");
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos), Charset.forName("GBK"));
byte[] bufs = new byte[1024*10];
for(int i=0; i<fileList.size(); i++){
//创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(new String(fileList.get(i).getName().getBytes("GBK"),"GBK"));
zos.putNextEntry(zipEntry);
//读取待压缩的文件并写进压缩包里
bis = new BufferedInputStream(fileList.get(i).getInputstream(), 1024*10);
int read = 0;
while((read=bis.read(bufs, 0, 1024*10)) != -1){
zos.write(bufs,0,read);
}
}
log.info(zipFilePath + "目录下写入压缩包完毕");
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally{
//关闭流
try {
if(null != bis) bis.close();
if(null != zos) zos.close();
byte[] filebyte = Filebyte(zipFile);
// 删除zip文件夹
zipFile.delete();
return filebyte;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static byte[] Filebyte(File file){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
long fileSize = file.length();
log.info("File size is " + fileSize + " bytes");
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
}