Here are the examples of the java api com.blade.mvc.ui.RestResponse.builder() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
15
Source : AttachController.java
with MIT License
from tfssweb
with MIT License
from tfssweb
/**
* 上传文件接口
* <p>
* 返回格式
*
* @param request
* @return
*/
@Route(value = "upload", method = HttpMethod.POST)
@JSON
public RestResponse upload(Request request) {
log.info("UPLOAD DIR = {}", TaleUtils.UP_DIR);
Users users = this.user();
Integer uid = users.getUid();
Map<String, FileItem> fileItemMap = request.fileItems();
Collection<FileItem> fileItems = fileItemMap.values();
List<Attach> errorFiles = new ArrayList<>();
List<Attach> urls = new ArrayList<>();
try {
fileItems.forEach((FileItem f) -> {
String fname = f.getFileName();
if ((f.getLength() / 1024) <= TaleConst.MAX_FILE_SIZE) {
String fkey = TaleUtils.getFileKey(fname);
String ftype = f.getContentType().contains("image") ? Types.IMAGE : Types.FILE;
String filePath = TaleUtils.UP_DIR + fkey;
try {
Files.write(Paths.get(filePath), f.getData());
} catch (IOException e) {
log.error("", e);
}
Attach attach = new Attach();
attach.setFname(fname);
attach.setAuthor_id(uid);
attach.setFkey(fkey);
attach.setFtype(ftype);
attach.setCreated(DateKit.nowUnix());
attach.save();
urls.add(attach);
siteService.cleanCache(Types.C_STATISTICS);
} else {
Attach attach = new Attach();
attach.setFname(fname);
errorFiles.add(attach);
}
});
if (errorFiles.size() > 0) {
return RestResponse.builder().success(false).payload(errorFiles).build();
}
return RestResponse.ok(urls);
} catch (Exception e) {
String msg = "文件上传失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
log.error(msg, e);
}
return RestResponse.fail(msg);
}
}