java - java.io.FileNotFoundException 通过使用 Spring RestTemplate 上传具有文件名的文件
问题描述
我正在上传一个带有文件名的文件,下面是我的代码;
我的代码如下;
String filePath = "/C:/../../promoCode/" + fileName + ".txt";
File targetFile = new File(filePath);
FileWriter writer = new FileWriter(targetFile);
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write(generatedVoucherText);
String bucketName = "promocodes";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
ContentDisposition contentDisposition = ContentDisposition
.builder("form-data")
.name("file")
.filename(fileName)
.build();
fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
FileReader reader = new FileReader(targetFile);
BufferedReader bufferedReader = new BufferedReader(reader);
String someByteArray = bufferedReader.readLine();
HttpEntity<byte[]> requestEntity = new HttpEntity<>(someByteArray.getBytes(StandardCharsets.UTF_8), fileMap);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", requestEntity);
HttpEntity<MultiValueMap<String, Object>> httpEntity =
new HttpEntity<>(body, headers);
minioService.postMinio2(httpEntity, bucketName);
buffer.close();
我不知道为什么我有 I/O 异常。
FullStackTrace 发生异常日志详细信息:“http://sth-sth.apps.mbt.sth.local promocodes”的 POST 请求出现 I/O 错误:类路径资源 [C:/../../promoCode/ABF. txt] 无法解析为 URL,因为它不存在;嵌套异常是 java.io.FileNotFoundException:类路径资源 [C:/../../promoCode/ABF.txt] 无法解析为 URL,因为它不存在,类:类 org.springframework.web.client。资源访问异常
谢谢
解决方案
推荐阅读
- java - 关于java中的日期格式
- r - 如何在左括号前插入空格
- laravel - 如何通过 OAuth 登录,以便 cron 作业可以运行 Laravel
- python - 从数据框中读取信息
- .net - C# 类库“使程序集 COM 可见”灰色或不可用
- ios - 通过 MethodChannel 从本机 iOS 端收到消息后,Flutter 活动未导航
- firebase - Firestore 中的最大数组大小是多少?
- javascript - 为什么使用默认导入而不是命名导入时我的 ES6 webpack 包更大?
- postgresql - 在另一列中更新并存储先前的值
- python - 训练模型时 Keras 输出数据中显示的 90/90 是多少?