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