乱码,路径问题顺带解决了,重复下载问题也解决了。
Map params = (Map) JSON.parse(data);
/*用工具类把参数转换后的Map再转换成实体类对象*/
InfoAttach attach=beanToMapUtils.toBean(InfoAttach.class,params);
try {
URL url = new URL(attach.getFileUrl());
// 打开和URL之间的连接
URLConnection conn = url.openConnection();
// 设置通用的请求属性
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setConnectTimeout(80000);
conn.setReadTimeout(80000);
//Post 请求不能使用缓存
conn.setUseCaches(false);
//调用URLConnection对象提供的connect方法连接远程服务
conn.connect();
System.out.println("object:"+conn.getContent());
//通过URL打开了通道,拿到了输入流对象
InputStream inStream = conn.getInputStream();
//创建缓冲流对象加强功能
//BufferedInputStream bis =new BufferedInputStream(inStream);
int bytesum = 0;
int byteread = 0;
//byte[] buffer = new byte[1204];
int count=conn.getContentLength();
byte[] bytes=new byte[count];
int readCount= 0;
while (readCount<count){
readCount += inStream.read(bytes,readCount,count-readCount);
}
//bis.read(bytes);
attach.setFileSize(conn.getContentLength());
attach.setBytes(bytes);
//关闭流
//bis.close();
inStream.close();
}catch (Exception e){
e.printStackTrace();
}
return InterfaceResult.success(attach);
String fileName = attach.getPk_info() + "-" + attach.getFileName();
//String filePath = Paths.get(rootDirectory,configProperties.getDownloadDir(), fileName).toString();
String classpath = this.getClass().getResource("/").getPath().replaceFirst("/", "");
String docRoot = classpath.replaceAll("/target/classes/", configProperties.getDownloadDir());
String filePath=docRoot+ "/" + fileName;
//1.指定要写到的文件目录及名称
File dest = new File(filePath);
//System.out.println(dest.getCanonicalPath());
if (dest.exists()) {
FileInputStream fis = new FileInputStream(dest);
attach.setFileSize(fis.available());
fis.close();
return;
}
/*URL url = new URL(attach.getFileUrl());
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();*/
/*调用jt端,先把对象转换成map,然后当参数*/
Map<String,Object> dataMap=new HashMap<>();
dataMap=beanToMapUtils.toMap(attach);
String interfaceResult=mobileOAInterfaceService.oaInterfaceAccess(dataMap,"downloadFromNc");
/*返回的字符串里有个data*/
//InterfaceResult resultZh=JSON.parseObject(interfaceResult,InterfaceResult.class);
//String daiZhuan=JSON.toJSONString(resultZh.getData());
InfoAttach infoAttachZh=JSON.parseObject( interfaceResult ,InfoAttach.class);
//2.创建文件读入流对象
OutputStream fos =new FileOutputStream(dest);
fos.write(infoAttachZh.getBytes());
//3.创建缓冲流对象加强fos功能
//BufferedOutputStream bos=new BufferedOutputStream(fos);
//4.向流中写入数据
//byte[] bytes=infoAttachZh.getFileInputStreamObj().getBytes();
//byte[] bytes=infoAttachZh.getBytes();
//5.刷新和关闭流
//bos.flush();
//bos.close();
fos.close();
return;
//1.指定要写到的文件目录及名称,filePath就是索引,文件夹路径+文件名字
File dest = new File(filePath);
System.out.println(dest.getCanonicalPath());
if (dest.exists()) {
FileInputStream fis = new FileInputStream(dest);
attach.setFileSize(fis.available());
fis.close();
return;
}