本文整理匯總了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;
}