use of com.blade.mvc.multipart.FileItem in project blade by biezhi.
the class MethodArgument method getArgs.
public static Object[] getArgs(Request request, Response response, Method actionMethod) throws Exception {
Class<?>[] parameters = actionMethod.getParameterTypes();
Annotation[][] annotations = actionMethod.getParameterAnnotations();
Object[] args = new Object[parameters.length];
actionMethod.setAccessible(true);
String[] paramaterNames = AsmKit.getMethodParamNames(actionMethod);
for (int i = 0, len = parameters.length; i < len; i++) {
Class<?> argType = parameters[i];
if (argType == Request.class) {
args[i] = request;
continue;
}
if (argType == Response.class) {
args[i] = response;
continue;
}
if (argType == Session.class) {
args[i] = request.session();
continue;
}
if (argType == ModelAndView.class) {
args[i] = new ModelAndView();
continue;
}
if (argType == Map.class) {
args[i] = request.querys();
continue;
}
Annotation annotation = annotations[i][0];
if (null != annotation) {
// query param
if (annotation.annotationType() == QueryParam.class) {
QueryParam queryParam = (QueryParam) annotation;
String paramName = queryParam.value();
String val = request.query(paramName);
boolean required = queryParam.required();
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.query(paramName);
}
if (StringKit.isBlank(val)) {
val = queryParam.defaultValue();
}
if (required && StringKit.isBlank(val)) {
throw new RouteException("query param [" + paramName + "] not is empty.");
}
args[i] = getRequestParam(argType, val);
continue;
}
// path param
if (annotation.annotationType() == PathParam.class) {
PathParam pathParam = (PathParam) annotation;
String paramName = pathParam.value();
String val = request.pathParam(paramName);
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.pathParam(paramName);
}
if (StringKit.isBlank(val)) {
val = pathParam.defaultValue();
}
args[i] = getRequestParam(argType, val);
}
// header param
if (annotation.annotationType() == HeaderParam.class) {
HeaderParam headerParam = (HeaderParam) annotation;
String paramName = headerParam.value();
String val = request.header(paramName);
boolean required = headerParam.required();
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.header(paramName);
}
if (StringKit.isBlank(val)) {
val = headerParam.defaultValue();
}
if (required && StringKit.isBlank(val)) {
throw new RouteException("header param [" + paramName + "] not is empty.");
}
args[i] = getRequestParam(argType, val);
continue;
}
// cookie param
if (annotation.annotationType() == CookieParam.class) {
CookieParam cookieParam = (CookieParam) annotation;
String paramName = cookieParam.value();
String val = request.cookie(paramName);
boolean required = cookieParam.required();
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.cookie(paramName);
}
if (StringKit.isBlank(val)) {
val = cookieParam.defaultValue();
}
if (required && StringKit.isBlank(val)) {
throw new RouteException("cookie param [" + paramName + "] not is empty.");
}
args[i] = getRequestParam(argType, val);
continue;
}
// form multipart
if (annotation.annotationType() == MultipartParam.class && argType == FileItem.class) {
MultipartParam multipartParam = (MultipartParam) annotation;
String paramName = multipartParam.value();
FileItem val = request.fileItem(paramName);
if (StringKit.isBlank(paramName)) {
assert paramaterNames != null;
paramName = paramaterNames[i];
val = request.fileItem(paramName);
}
args[i] = val;
continue;
}
}
}
return args;
}
use of com.blade.mvc.multipart.FileItem in project blade by biezhi.
the class ServletRequest method init.
public void init() throws IOException, MultipartException {
// retrieve multipart/form-data parameters
if (Multipart.isMultipartContent(request)) {
Multipart multipart = new Multipart();
multipart.parse(request, new MultipartHandler() {
@Override
public void handleFormItem(String name, String value) {
queryParams.put(name, value);
}
@Override
public void handleFileItem(String name, FileItem fileItem) {
fileItems.put(name, fileItem);
}
});
}
}
use of com.blade.mvc.multipart.FileItem in project blade by biezhi.
the class MethodArgumentN method getArgs.
public static Object[] getArgs(Request request, Response response, Method actionMethod) throws Exception {
int parameterCount = 0;
for (final Parameter parameter : actionMethod.getParameters()) {
System.out.println("\targ" + parameterCount++ + ": " + (parameter.isNamePresent() ? parameter.getName() : "Parameter Name not provided,") + (isParameterFinal(parameter) ? " IS " : " is NOT ") + "final, type " + parameter.getType().getCanonicalName() + ", and parameterized type of " + parameter.getParameterizedType() + " and " + (parameter.isVarArgs() ? "IS " : "is NOT ") + "variable.");
}
Parameter[] parameters = actionMethod.getParameters();
Object[] args = new Object[parameters.length];
actionMethod.setAccessible(true);
for (int i = 0, len = parameters.length; i < len; i++) {
Parameter parameter = parameters[i];
Class<?> argType = parameter.getType();
if (argType == Request.class) {
args[i] = request;
continue;
}
if (argType == Response.class) {
args[i] = response;
continue;
}
if (argType == ModelAndView.class) {
args[i] = new ModelAndView();
continue;
}
if (argType == Map.class) {
args[i] = request.querys();
continue;
}
QueryParam queryParam = parameter.getAnnotation(QueryParam.class);
if (null != queryParam) {
String paramName = queryParam.value();
String val = request.query(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.query(paramName);
}
if (StringKit.isBlank(val)) {
val = queryParam.defaultValue();
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
PathParam pathParam = parameter.getAnnotation(PathParam.class);
if (null != pathParam) {
String paramName = pathParam.value();
String val = request.pathParam(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.pathParam(paramName);
}
if (StringKit.isBlank(val)) {
throw new NotFoundException("path param [" + paramName + "] is null");
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
HeaderParam headerParam = parameter.getAnnotation(HeaderParam.class);
if (null != headerParam) {
String paramName = headerParam.value();
String val = request.header(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.header(paramName);
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
CookieParam cookieParam = parameter.getAnnotation(CookieParam.class);
if (null != cookieParam) {
String paramName = cookieParam.value();
String val = request.cookie(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.cookie(paramName);
}
args[i] = MethodArgument.getRequestParam(argType, val);
}
MultipartParam multipartParam = parameter.getAnnotation(MultipartParam.class);
if (null != multipartParam && argType == FileItem.class) {
String paramName = multipartParam.value();
FileItem val = request.fileItem(paramName);
if (StringKit.isBlank(paramName)) {
paramName = parameter.getName();
val = request.fileItem(paramName);
}
args[i] = val;
}
}
return args;
}
use of com.blade.mvc.multipart.FileItem in project tale by otale.
the class AttachController method upload.
/**
* 上传文件接口
*
* 返回格式
* @param request
* @return
*/
@Route(value = "upload", method = HttpMethod.POST)
@JSON
public RestResponse upload(Request request) {
LOGGER.info("UPLOAD DIR = {}", TaleUtils.upDir);
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.fileName();
if (f.file().length() / 1024 <= TaleConst.MAX_FILE_SIZE) {
String fkey = TaleUtils.getFileKey(fname);
String ftype = TaleUtils.isImage(f.file()) ? Types.IMAGE : Types.FILE;
String filePath = TaleUtils.upDir + fkey;
File file = new File(filePath);
try {
Tools.copyFileUsingFileChannels(f.file(), file);
f.file().delete();
} catch (IOException e) {
e.printStackTrace();
}
Attach attach = attachService.save(fname, fkey, ftype, uid);
urls.add(attach);
siteService.cleanCache(Types.C_STATISTICS);
} else {
errorFiles.add(new Attach(fname));
}
});
if (errorFiles.size() > 0) {
RestResponse restResponse = new RestResponse();
restResponse.setSuccess(false);
restResponse.setPayload(errorFiles);
return restResponse;
}
return RestResponse.ok(urls);
} catch (Exception e) {
String msg = "文件上传失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
LOGGER.error(msg, e);
}
return RestResponse.fail(msg);
}
}
Aggregations