本文整理汇总了Java中org.springframework.web.multipart.commons.CommonsMultipartResolver类的典型用法代码示例。如果您正苦于以下问题:Java CommonsMultipartResolver类的具体用法?Java CommonsMultipartResolver怎么用?Java CommonsMultipartResolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CommonsMultipartResolver类属于org.springframework.web.multipart.commons包,在下文中一共展示了CommonsMultipartResolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getResolver
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
@Bean(name = "multipartResolver")
public CommonsMultipartResolver getResolver() throws IOException{
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSizePerFile(5242880);
resolver.setMaxUploadSize(52428807);
resolver.setDefaultEncoding("UTF-8");
return resolver;
}
示例2: uploadFile
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 上传文件
* @param request
* @return
*/
public File uploadFile(HttpServletRequest request) {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
try {
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
String key = iterator.next();
MultipartFile multipartFile = multiRequest.getFile(key);
if (multipartFile != null) {
String name = multipartFile.getOriginalFilename();
String pathDir = request.getSession().getServletContext().getRealPath("/upload/" + DateUtils.currentTime());
File dirFile = new File(pathDir);
if (!dirFile.isDirectory()) {
dirFile.mkdirs();
}
String filePath = pathDir+File.separator+name;
File file = new File(filePath);
file.setWritable(true, false);
multipartFile.transferTo(file);
return file;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例3: multipartResolver
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
@Bean
public MultipartResolver multipartResolver(WikiConfig wikiConfig) {
final String UPLOAD_TEMP_DIR = wikiConfig.getUploadTempDir();
//
CommonsMultipartResolver mr = new CommonsMultipartResolver();
mr.setMaxUploadSize(MAX_UPLOAD_SIZE);
mr.setDefaultEncoding(DEFAULT_ENCODING);
try {
Resource uploadTempDir = resourceLoader
.getResource(UPLOAD_TEMP_DIR);
mr.setUploadTempDir(uploadTempDir);
LOG.debug(String.format("UPLOAD TEMP DIR = %s", uploadTempDir));
} catch (IOException e) {
LOG.error(String.format("#getResource(%s) FAIL, FALLBACK.",
UPLOAD_TEMP_DIR), e);
}
return mr;
}
示例4: fileUpload
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 压测文件上传
*
* @param request
* @param response
* @return
*/
@RequestMapping(value = "/fileUpload")
@ResponseBody
public ComponentResult<FileUploadBO> fileUpload(HttpServletRequest request,HttpServletResponse response) {
ComponentResult<FileUploadBO> componentResult = new ComponentResult<FileUploadBO>();
//1、获取上传参数
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile file = multiRequest.getFile(iterator.next().toString());
if (null != file) {
//2、上传
String filename = new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date()) + ".xls";
try {
File targetFile = new File(pressureDataFilepath + filename);
file.transferTo(targetFile);
//3、将本地文件上传到FTP
boolean uploadResult = ftpUtils.uploadFile(targetFile);
if(uploadResult){
componentResult.setData(new FileUploadBO(filename));
return ResultUtil.success(componentResult);
}else{
logger.error("文件上传FTP失败,filename:{}",filename);
return ResultUtil.fail(ErrorCode.FAIL,"文件上传FTP失败",componentResult);
}
} catch (IllegalStateException | IOException e) {
logger.error("上传文件异常,filename->" + filename,e);
}
}else{
logger.error("压测文件上传,file为NULL");
}
}
}else{
logger.error("压测文件上传,request校验失败");
}
//3、返回
return ResultUtil.fail(componentResult);
}
示例5: uploadFile
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/** 上传文件处理(支持批量) */
public static List<String> uploadFile(HttpServletRequest request) {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
List<String> fileNames = InstanceUtil.newArrayList();
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
String pathDir = getUploadDir(request);
File dirFile = new File(pathDir);
if (!dirFile.isDirectory()) {
dirFile.mkdirs();
}
for (Iterator<String> iterator = multiRequest.getFileNames(); iterator.hasNext();) {
String key = iterator.next();
MultipartFile multipartFile = multiRequest.getFile(key);
if (multipartFile != null) {
String name = multipartFile.getOriginalFilename();
String uuid = UUID.randomUUID().toString();
String postFix = name.substring(name.lastIndexOf(".")).toLowerCase();
String fileName = uuid + postFix;
String filePath = pathDir + File.separator + fileName;
File file = new File(filePath);
file.setWritable(true, false);
try {
multipartFile.transferTo(file);
fileNames.add(fileName);
} catch (Exception e) {
logger.error(name + "保存失败", e);
}
}
}
}
return fileNames;
}
示例6: multipartResolver
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxInMemorySize(40960);
resolver.setMaxUploadSize(50 * 1024 * 1024);//上传文件大小 50M 50*1024*1024
return resolver;
}
示例7: doPost
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
try {
MultipartHttpServletRequest
multiRequest =
new CommonsMultipartResolver().resolveMultipart(request);
MultiValueMap<String, MultipartFile> fileMap = multiRequest.getMultiFileMap();
BlobstoreService blobstoreService = AppFactory.get().getBlobstoreService();
Map<String, String> names = new HashMap<>(1);
for (String fieldName : fileMap.keySet()) {
MultipartFile file = fileMap.getFirst(fieldName);
String fileName = file.getOriginalFilename();
String contentType = file.getContentType();
long sizeInBytes = file.getSize();
String
blobKey =
blobstoreService.store(fileName, contentType, sizeInBytes, file.getInputStream());
names.put(fieldName, blobKey);
}
request.getSession().setAttribute("blobs", names);
RequestDispatcher
dispatcher =
getServletContext().getRequestDispatcher(request.getParameter("ru"));
dispatcher.forward(multiRequest, response);
} catch (Exception ex) {
_logger.severe("Upload failed", ex);
}
}
示例8: doPost
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
try {
MultipartHttpServletRequest
multiRequest =
new CommonsMultipartResolver().resolveMultipart(request);
MultiValueMap<String, MultipartFile> fileMap = multiRequest.getMultiFileMap();
BlobstoreService blobstoreService = AppFactory.get().getBlobstoreService();
Map<String, String> names = new HashMap<>(1);
for (String fieldName : fileMap.keySet()) {
MultipartFile file = fileMap.getFirst(fieldName);
String fileName = file.getOriginalFilename();
String contentType = file.getContentType();
long sizeInBytes = file.getSize();
String
blobKey =
blobstoreService.store(fileName, contentType, sizeInBytes, file.getInputStream());
names.put(fieldName, blobKey);
names.put("file", fileName);
}
response.getWriter().write(new Gson().toJson(names));
response.setStatus(HttpStatus.SC_CREATED);
} catch (Exception ex) {
_logger.severe("Upload failed", ex);
}
}
示例9: uploadFile
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 上传文件处理(支持批量)
* @param request
* @param pathDir 上传文件保存路径
* @return
* @throws IllegalStateException
* @throws IOException
*/
public static List<String> uploadFile(HttpServletRequest request,String pathDir) throws IllegalStateException, IOException {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
List<String> fileNames = InstanceUtil.newArrayList();
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
if(pathDir==null|| pathDir.equals("")){
pathDir = request.getSession().getServletContext().getRealPath(uploadFileDir + DateUtils.currentTime());
}
File dirFile = new File(pathDir);
if (!dirFile.isDirectory()) {
dirFile.mkdirs();
}
while (iterator.hasNext()) {
String key = iterator.next();
MultipartFile multipartFile = multiRequest.getFile(key);
if (multipartFile != null) {
String uuid = UUID.randomUUID().toString().replace("-", "");
String name = multipartFile.getOriginalFilename();
int lastIndexOf = name.lastIndexOf(".");
String postFix="";
if(lastIndexOf!=-1){
postFix = name.substring(lastIndexOf).toLowerCase();
}
String fileName = uuid + postFix;
String filePath = pathDir + File.separator + fileName;
File file = new File(filePath);
file.setWritable(true, false);
multipartFile.transferTo(file);
fileNames.add(file.getAbsolutePath());
}
}
}
return fileNames;
}
示例10: multipartResolver
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
@Bean
public CommonsMultipartResolver multipartResolver()
{
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
return resolver;
}
示例11: multipartResolver
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
@Bean
public CommonsMultipartResolver multipartResolver() {
final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setMaxUploadSize(-1);
commonsMultipartResolver.setDefaultEncoding("UTF-8");
return commonsMultipartResolver;
}
示例12: uploadFile
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 上传文件处理(支持批量)
* @param request
* @param pathDir 上传文件保存路径
* @return
* @throws IllegalStateException
* @throws IOException
*/
public static List<String> uploadFile(HttpServletRequest request,String pathDir) throws IllegalStateException, IOException {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
List<String> fileNames = new ArrayList<String>();
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> iterator = multiRequest.getFileNames();
if(pathDir==null|| pathDir.equals("")){
pathDir = request.getSession().getServletContext().getRealPath(uploadFileDir + DateUtils.currentTime());
}
File dirFile = new File(pathDir);
if (!dirFile.isDirectory()) {
dirFile.mkdirs();
}
while (iterator.hasNext()) {
String key = iterator.next();
MultipartFile multipartFile = multiRequest.getFile(key);
if (multipartFile != null) {
String uuid = UUID.randomUUID().toString().replace("-", "");
String name = multipartFile.getOriginalFilename();
int lastIndexOf = name.lastIndexOf(".");
String postFix="";
if(lastIndexOf!=-1){
postFix = name.substring(lastIndexOf).toLowerCase();
}
String fileName = uuid + postFix;
String filePath = pathDir + File.separator + fileName;
File file = new File(filePath);
file.setWritable(true, false);
multipartFile.transferTo(file);
fileNames.add(file.getAbsolutePath());
}
}
}
return fileNames;
}
示例13: upload
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 上传附件
* @param request
* @return
* @throws IllegalStateException
* @throws IOException
*/
public String upload(HttpServletRequest request) throws IllegalStateException, IOException {
ResourceBundle projectConfig = ResourceBundle.getBundle("config");
String filePath=projectConfig.getString("osframe.file.path");
//创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
//判断 request 是否有文件上传,即多部分请求
if(multipartResolver.isMultipart(request)){
//转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
//取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while(iter.hasNext()){
//记录上传过程起始时的时间,用来计算上传时间
int pre = (int) System.currentTimeMillis();
//取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if(file != null){
//取得当前上传文件的文件名称
String myFileName = file.getOriginalFilename();
//如果名称不为“”,说明该文件存在,否则说明该文件不存在
if(myFileName.trim() !=""){
System.out.println(myFileName);
//重命名上传后的文件名
String fileName = "demoUpload\\" + file.getOriginalFilename();
//定义上传路径
String path = filePath+ fileName;//"E:\\upload\\"
File localFile = new File(path);
file.transferTo(localFile);
}
}
//记录上传该文件后的时间
int finaltime = (int) System.currentTimeMillis();
System.out.println(finaltime - pre);
}
}
return "/success";
}
示例14: upload
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 上传文件
* @param request
* @param dir
* @return
* @throws IllegalStateException
* @throws IOException
*/
public List<File> upload(File dir) throws IllegalStateException, IOException {
List<File> result = new ArrayList<File>();
HttpServletRequest request = getRequest();
// 创建一个通用的多部分解析器
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
// 判断 request 是否有文件上传,即多部分请求
if (multipartResolver.isMultipart(request)) {
// 转换成多部分request
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
// 取得request中的所有文件名
Iterator<String> iter = multiRequest.getFileNames();
while (iter.hasNext()) {
// 取得上传文件
MultipartFile file = multiRequest.getFile(iter.next());
if (file != null) {
// 取得当前上传文件的文件名称
String fileName = file.getOriginalFilename();
// 如果名称不为"",说明该文件存在,否则说明该文件不存在
if (BasicUtil.isNotEmpty(fileName)) {
// 重命名上传后的文件名
String sufName = FileUtil.getSuffixFileName(fileName);
// 定义上传路径
File localFile = new File(dir,BasicUtil.getRandomLowerString(10)+"."+sufName);
file.transferTo(localFile);
result.add(localFile);
}
}
}
}
return result;
}
示例15: upload2
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入依赖的package包/类
/**
* 向当前运行的war中上传文件
* @param upPath
* @param request
* @param response
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping("/upload")
public String upload2(HttpServletRequest request, HttpServletResponse response)
throws IllegalStateException, IOException {
// @RequestParam String upPath ,
log.info("upPath:[{}][{}]", request.getParameter("upPath"), request.getAttribute("upPath"));
String upPath = "/tpl/";
log.info("username[{}],upPath[{}]开始上传文件", SessionUtil.getUserInfo().getMember().getAccNo(), upPath);
String rootPath = request.getSession().getServletContext().getRealPath("/");
log.debug("获取到项目根路径为rootPath[{}]", rootPath);
ShiroHttpServletRequest shiroRequest = (ShiroHttpServletRequest) request;
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart((HttpServletRequest) shiroRequest.getRequest());
Iterator<String> itr = multipartRequest.getFileNames();
MultipartFile file = null;
while (itr.hasNext()) {
file = multipartRequest.getFile(itr.next());
long pre = System.currentTimeMillis();
if (file != null) {
if (StringUtils.isNotBlank(file.getOriginalFilename())) {
String fileName = StringUtil.combinationString(upPath, "/", file.getOriginalFilename());
String path = rootPath + fileName;
File localFile = new File(path);
file.transferTo(localFile);
}
}
long finaltime = System.currentTimeMillis();
log.info("上传文件耗时:{}" , (finaltime - pre));
}
return "file/success";
}