郵便配達員またはアプリケーションのUIから複数の書式/データ形式のファイル(書式doc、docx、pdf、textなど)をREST APIにアップロードしています。テキストファイルは正常にアップロードされます。その他の非テキスト形式はすべて破損します。私はそれらのファイルを開くことができません。REST APIによるマルチファイルアップロードによりファイルが破損し、ファイルサイズが増加する
アップロードされるファイルのサイズが大幅に増加します。次のサーバーのログをチェック:
File size just before call to request.getRequestDispatcher().forward(): 27583
Controller, first line in method:39439
をアップロードされたファイルのサイズは、私は、ファイルにあるため、ファイルに追加その他のデータの破損したと推測してい27.3Kb
です。テキストファイルが正しく保存され、他のファイルも正しく書き込まれますので
コントローラのメソッドが
@RequestMapping(value="/entity/{entity}/{entityId}/type/{type}/{derive}",method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile multipartFile,@PathVariable("entity")String entity,@PathVariable("type")String type,@PathVariable("entityId")Long entityId,@PathVariable("derive") boolean derive) throws Exception
で、ファイルを書き込むためのコードが間違っているとは思いません。
コードは、InputStreamの
public String storeFile(MultipartFile multipartFile, String entity, Long id, String uploadType, boolean isDerive,String customName)
throws Exception
{
try
{
System.out.println(multipartFile.getSize());
String fileName = "";
String contentType = "";
if (multipartFile != null)
{
fileName = multipartFile.getOriginalFilename();
contentType = multipartFile.getContentType();
if (contentType == null)
{
contentType = "application/msword";
}
}
InputStream is = multipartFile.getInputStream();
String filePath = getFileName(entity, uploadType, id, fileName, isDerive,customName);
Helper.storeFile(is, filePath);
precedingPath = precedingPath.length() > 0 ? precedingPath + "/":"";
return precedingPath + filePath;
}
catch (WebException e)
{
e.printStackTrace();
throw e;
}
catch (Exception e)
{
e.printStackTrace();
throw new WebException(e.getMessage(), IHttpConstants.INTERNAL_SERVER_ERROR, e);
}
}
Helper.storeFileを取得するにはアップロード中
var fd = new FormData();
//Take the first selected file
fd.append("file", document.actualFile);
//Generic AJAX call
CandidateService.ajax_uploadDocumentWithDocType($scope.candidate.id, fd, document.docType, function (error, json)
コンテンツの種類を次のように
public static File storeFile(InputStream is, String filePath) throws IOException {
try {
String staticRepoPath = null;
if (MasterData.getInstance().getSettingsMap().containsKey(Settings.REPO_LOCATION.toString())) {
staticRepoPath = MasterData.getInstance().getSettingsMap().get(Settings.REPO_LOCATION.toString());
} else {
throw new WebException("Invalid Settings");
}
byte[] buffer = new byte[is.available()];
is.read(buffer);
File targetFile = new File(staticRepoPath + File.separator + filePath);
@SuppressWarnings("resource")
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
return targetFile;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
私のAjaxリクエストは、次のとおりです。
var config = {headers:{'X-Auth-Token':authToken, 'Content-Type': undefined}, transformRequest: angular.identity};
どのように私はこれを修正し、ファイルを正常にアップロードできるか知っていますか?
Q1)要求ディスパッチャとファイルデータを処理するコントローラ間でファイルサイズが変わるのはなぜですか。
Q2)このファイルサイズが変更されてファイルが破損する可能性がありますか? Libre Officeが原因で一般的な入出力エラーが発生する。
-1とマークされた理由を知りません。 私はこのAPIを1年以上使用しています。過去3ヶ月以降、アップロード時にファイルが壊れてしまいます。 gitの履歴を使ってコードを完全にチェックしてください。何が変わったのか分からない。したがって、ファイルサイズなどを確認するためにさらに深くなりました。 APIコールは – Sonal
に更新されました他のAPIで試しましたか? –
私はdownvoteをしませんでしたが、コードを投稿しませんでした。その 'MultipartFile'であなたが何をしているのかを知る方法はありません。 – chrylis