添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

解决 :关闭对应的流

private String sendReportToNethospital(Endoscopicreport endoscopicreport) throws IOException {
		// 获取外链接(网络)文件,以java.net.URL的方式获取外链接文件
		String outerLink = "http://文件的外网链接地址/";
		String serverPath = outerLink + endoscopicreport.getReportAddress();
		URL url = new URL(serverPath);
		URLConnection connection = url.openConnection();
		InputStream inputStream = connection.getInputStream();
		String locationPath = serviceConfig.getLocationPath()+endoscopicreport.getRepotName();
		File dir = new File(serviceConfig.getLocationPath());
		if (!dir.exists()) {
			dir.mkdirs();
		File file = new File(locationPath);
		FileOutputStream outputStream = new FileOutputStream(file);
		IOUtils.copy(inputStream, outputStream);
		// 通过指定header和body来创建HttpEntity
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
		// FileSystemResource有多个构造返方法
		body.add("file", new FileSystemResource(file)); // file值为文件服务器上传接口的参数
		HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
		// 调用template.postForEntity( )完成连接建立并发送文件到服务器
		String netHospitalFileInterface = serviceConfig.getNetHospitalFileUrl();
		ResponseEntity<String> response = template.postForEntity(netHospitalFileInterface, requestEntity, String.class);
		String data = response.getBody();
		// 将netHospitalFileInterface接口地址返回的数据存储到表nethospital_relation中
		nethospitalRelationMapper.updateFileIdByCheckno(data,endoscopicreport.getCheckNo());
		// 删除locationPath中的临时文件 ,删除前先关闭流
		inputStream.close();
		outputStream.close();
		new File(locationPath).delete();
		return null;

如上代码,倒数第3行需要关闭输出流outputStream后, 倒数第2行delete()方法才会到达预期的删除文件的效果。

file.delete()无法删除文件的原因及解决方法发布时间:2020-05-06 09:41:00来源:亿速云阅读:756作者:小新今天小编给大家分享的是file.delete()无法删除文件的原因及解决方法,相信很多人都不太了解,为了让大家更加了解file.delete(),所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。1、问题file.delete()无法删除文件file.d... public static void main(String[] args) { String rootDir = "D:\\codeware"; String delDirName = "io"; DelDir del = new DelDir(); del.FindDirectory(rootDir,delDirName); private void FindDirectory(String root,String delName){ //root是根目录的绝对路径 File directory = new File(root); File[] files = directory.listFiles(); //根目录为空 if(files.length ==0){ System.out.println(root+"根目录为空!"); return; else{ for(int i=0;i<files.length;i++){ if(files[i].isDirectory()){ //根目录的子文件夹就是要删除文件夹 if(files[i].getName().equals(delName)){ System.out.println(files[i].getAbsolutePath()); DelDirectory(files[i]); //根目录的子文件夹不是要删除文件夹 else{ FindDirectory(files[i].getAbsolutePath(),delName); private void DelDirectory(File directory){ File[] children = directory.listFiles(); if(children.length==0){ directory.delete(); System.out.println("\t删除目录"+directory.getName()); else{ for(int i=0;i<children.length;i++){ if(children[i].isFile()){ children[i].delete(); System.out.println("\t删除文件"+children[i].getName()); else if(children[i].isDirectory()){ DelDirectory(children[i]); System.out.println("\t"+children[i].getAbsolutePath()); directory.delete(); System.out.println("\t删除目录"+directory.getName()); .headers(headers) .contentLength(contentLength) .contentType(MediaType.APPLICATION_OCTET_ST... 疑问:1.为什么调用file.delete()方法时,返回值为false.2.为什么调用Guava工具jar包中的Files.move(from,to) ,报异常:java.io.IOException: Unable to delete执行代码程序前需要创建一个test.txt文件。上代码:packageindi.johnny.test007;importjava.io.File;importj... Java无法删除被占用资源文件解决办法(IO流的关闭) 笔者最近在debug一个小Java软件的时候,发现该软件的删除功能有点小问题 -- 删除功能不能删除文件删除错误提示为:操作无法完成 因为文件已在 Java(TM) Platform SE binary 中打开。 出现该问题的原因是创建的IO流没有正常的关闭,导致资源文件一直被占用! 只要为创建的每一个IO流加上close方法就能正常