我现在想做一个文件上传,因为要考虑部署多台tomcat,所以想把上传后的请求转发到一个统一的文件上传服务中,这样不会出现上传后找不到的问题, 代码如下,目前redirectReq是true 走的是bgUpload方法
private Result commonUpload(TempFile tmpFile){
Map<String, Object> data = new HashMap<String, Object>();
try {
File fromFile = tmpFile.getFile();
String tmpName = tmpFile.getSubmittedFileName();
String fileName = "";
if(redirectReq){
fileName = bgUpload(fromFile);
}else{
fileName = generateFileName(tmpName);
File toFile = new File(uploadPath + fileName);
if (!toFile.exists()) {
toFile.getParentFile().mkdirs();
toFile.createNewFile();
}
copyFile(fromFile, toFile);
}
data.put("fileName", fileName.replaceAll("\\\\", "/"));
data.put("orginName", tmpName);
Attachment attachment = new Attachment();
attachment.setFileName(fileName);
attachment.setFileOriginalName(tmpName);
if (!Lang.isEmpty(getUser()) && !Strings.isEmpty(getUser().getLoginname())) {
attachment.setUserName(getUser().getLoginname());
}
if (!Lang.isEmpty(getUser()) && !Strings.isEmpty(getUser().getLoginIp())) {
attachment.setUploadIp(getUser().getLoginIp());
}
attachment.setFileExtName(getFileType(fileName));
attachment.setFilePath(uploadPath + fileName);
attachment.setFileSize(getFileSize(tmpFile));
attachment.setUrl(downloadUrl + "?name=" + fileName.replaceAll("\\\\", "/"));
attachmentService.insert(attachment);
return Result.success("system.upload.success", data);
} catch (Exception e) {
e.printStackTrace();
log.errorf("方法为commonUpload,错误信息为:%s", e);
return Result.error("system.upload.error");
}
}
bgUpload的方法是这样的
private String bgUpload(File file){
Request req = Request.create(uploadUrl, METHOD.POST);
Map<String, Object> params = req.getParams();
params.put("file", file);
FilePostSender sender = new FilePostSender(req);
Response resp = sender.send();
return resp.getContent();
}
我想问下这样如果对于大文件,一次性从http输入流中还没有把文件流读完,直接在bgUpload中调用方法把file传递过去是否存在问题?如果有问题该怎么处理?我传递几M图片的时候没有问题,传递接近1个G我这里测试没有问题,但是客户那边上传总是失败,不知道什么原因。