本文整理汇总了Java中org.springframework.web.multipart.commons.CommonsMultipartResolver.isMultipart方法的典型用法代码示例。如果您正苦于以下问题:Java CommonsMultipartResolver.isMultipart方法的具体用法?Java CommonsMultipartResolver.isMultipart怎么用?Java CommonsMultipartResolver.isMultipart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.multipart.commons.CommonsMultipartResolver
的用法示例。
在下文中一共展示了CommonsMultipartResolver.isMultipart方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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);
}
示例3: 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;
}
示例4: 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;
}
示例5: 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;
}
示例6: 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";
}
示例7: 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;
}
示例8: Request
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入方法依赖的package包/类
public Request(HttpServletRequest servletRequest, ApplicationInstance application) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("Content-Type: " + servletRequest.getContentType() + ", charset=" + servletRequest.getCharacterEncoding());
}
this.servletRequest = servletRequest;
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
if (multipartResolver.isMultipart(servletRequest)) {
MultipartHttpServletRequest mpr = multipartResolver.resolveMultipart(servletRequest);
this.servletRequest = mpr;
body = mpr.getFileMap();
} else if (StringUtils.startsWithIgnoreCase(this.servletRequest.getContentType(), "application/json")) {
byte[] bytes = IOUtils.toByteArray(this.servletRequest.getInputStream());
String charset = this.servletRequest.getCharacterEncoding();
if (StringUtils.isEmpty(charset)) {
charset = application.getApplication().getCharsetEncoding();
}
String s = new String(bytes, charset);
ObjectMapper mapper = new ObjectMapper();
body = mapper.readValue(s, Object.class);
}
requestAttributeMap = new RequestAttributeMap(this.servletRequest);
requestHeaderMap = new RequestHeaderMap(this.servletRequest);
requestParameterMap = new RequestParameterMap(this.servletRequest);
}
示例9: uploadFiles
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入方法依赖的package包/类
/** 上传文件处理(支持批量) */
public static List<FileInfo> uploadFiles(HttpServletRequest request) {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
List<FileInfo> 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) {
FileInfo fileInfo = new FileInfo();
String name = multipartFile.getOriginalFilename();
fileInfo.setOrgName(name);
if (name.indexOf(".") == -1 && "blob".equals(name)) {
name = name + ".png";
}
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);
fileInfo.setFileSize(multipartFile.getSize());
try {
multipartFile.transferTo(file);
fileInfo.setFileName(fileName);
fileNames.add(fileInfo);
} catch (Exception e) {
logger.error(name + "保存失败", e);
}
}
}
}
return fileNames;
}
示例10: uploadImage
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入方法依赖的package包/类
/** 上传文件处理(支持批量)
* @throws IOException
* @throws IllegalStateException */
public static List<String> uploadImage(HttpServletRequest request) 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();
String 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 name = multipartFile.getOriginalFilename();
// 默认文件格式为png图片
if (name.indexOf(".") == -1 && "blob".equals(name)) {
name = name + ".png";
}
String uuid = UUID.randomUUID().toString().replace("-", "");
int lastIndexOf = name.lastIndexOf(".");
if(lastIndexOf==-1){
name=name + ".png";
}
lastIndexOf = name.lastIndexOf(".");
String 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;
}
示例11: uploadImage
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入方法依赖的package包/类
/** 上传文件处理(支持批量)
* @throws IOException
* @throws IllegalStateException */
public static List<String> uploadImage(HttpServletRequest request) 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();
String 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 name = multipartFile.getOriginalFilename();
// 默认文件格式为png图片
if (name.indexOf(".") == -1 && "blob".equals(name)) {
name = name + ".png";
}
String uuid = UUID.randomUUID().toString().replace("-", "");
int lastIndexOf = name.lastIndexOf(".");
if(lastIndexOf==-1){
name=name + ".png";
}
lastIndexOf = name.lastIndexOf(".");
String 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;
}
示例12: saveMultipartFiles
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入方法依赖的package包/类
/**
* 多文件上传保存
* @param request
* @return
*/
public static Map<String, File> saveMultipartFiles(HttpServletRequest request, String phone) {
Map<String, File> files = new HashMap<String, File>();
//创建一个通用的多部分解析器
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 multiFile = multiRequest.getFile(iter.next());
if (multiFile != null && !multiFile.isEmpty()) {
// 重命名上传后的文件名
String fileName = FileUtils.getRandomFileName(multiFile.getOriginalFilename());
// 定义上传路径
File uploadDir = new File(FileUtils.getRootPath(), "uploads");
uploadDir = new File(uploadDir, phone);
uploadDir.mkdir();
// 接收并保存上传的文件
final File uploadFile = new File(uploadDir, fileName);
try {
multiFile.transferTo(uploadFile);
files.put(multiFile.getName(), uploadFile);
// 异步执行图片压缩
executor.execute(new Runnable() {
@Override
public void run() {
Pic pic = new Pic(uploadFile);
pic.resizeBy(Config.MAX_IMAGE_WIDTH, Config.MAX_IMAGE_HEIGHT);
pic.save();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return files;
}
示例13: uploadPicture
import org.springframework.web.multipart.commons.CommonsMultipartResolver; //导入方法依赖的package包/类
/**
* 批量上传图片
*
* @param request
* @param picturePath
* 图片的存放路径(服务器上的路径)
* @param isDelFile
* 错误时是否删除文件夹 1:删除 0:不删除,只删除文件
* @return serverPhotos == null 表示没有图片<br>
* serverPhotos == "" 表示上传图片失败 <br>
* 否则表示成功
* @throws IllegalStateException
* @throws IOException
*/
protected String uploadPicture(HttpServletRequest request,
String picturePath, int isDelFile) throws IllegalStateException,
IOException {
// 服务器上的图片路径
String serverPhotos = null;
CommonsMultipartResolver resolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
try {
if (resolver.isMultipart(request)) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
Iterator<String> it = multipartHttpServletRequest
.getFileNames();
while (it.hasNext()) {
MultipartFile file = multipartHttpServletRequest.getFile(it
.next());
if (!file.isEmpty()) {
// 原图名称
String newFileName = methodUtil.getNewFileName();
// 小图名称
String smallFileName = methodUtil
.getSmallPicName(newFileName);
// 本地路径
String path = methodUtil.creatAndGetRealPath(
picturePath, request);
// 本地上的原图路径(用于发布失败后删除本地图片)
String localFileName = path + newFileName;
// 本地上的小图路径(用于发布失败后删除本地图片)
// String localSmallFileName = path + smallFileName;
// 服务器上的路径
String serverPhoto = CommonVariables.SERVER_NAME
+ picturePath + "/" + smallFileName;
// 将文件写入本地硬盘
File localFile = new File(localFileName);
file.transferTo(localFile);
// 多图路径
serverPhotos = serverPhotos
+ CommonVariables.PICTURE_URL_FLAG
+ serverPhoto;
// 调用小图处理工具
boolean picFlag = this.getSmallPicture(
localFileName,
path,
smallFileName.substring(0,
smallFileName.lastIndexOf(".")));
// 发生错误则删除已上传的图片
if (picFlag == false) {
log.error("图片处理失败!");
this.deletePictureByServerPath(serverPhotos,
picturePath, isDelFile, request);
serverPhotos = "";
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
return serverPhotos;
}