MultipartFile.getsize的单位
时间: 2024-06-14 08:02:29 浏览: 13
MultipartFile.getsize() 方法在计算文件大小时,通常使用字节(Byte)作为单位。这是因为在计算机中,文件大小通常以字节为单位进行测量。字节是计算机数据的基本单位,它可以表示各种不同的数据类型,如文本、图像、音频和视频等。
请注意,虽然字节是最常用的文件大小单位,但在某些情况下,其他单位,如千字节(KB)、兆字节(MB)或吉字节(GB)等,可能会被使用。这些单位通常用于描述更大的数据集或更高级别的计算。
总之,MultipartFile.getsize() 方法返回的文件大小通常以字节为单位。然而,在某些情况下,可能会使用其他单位,如千字节、兆字节或吉字节等。
相关问题
@RequestMapping({"/upload"}) public String fileUpload0(MultipartFile[] files, HttpServletRequest request, HttpSession session) throws IOException { if (files != null && files.length > 0) { int filesTotal = files.length; String savePathRoot = request.getServletContext().getRealPath("/uploadFiles"); for(int i = 0; i < filesTotal; ++i) { FileInfo fileInfo = new FileInfo(); MultipartFile multipartFile = files[i]; if (multipartFile != null) { String originalFilename = multipartFile.getOriginalFilename(); String newFileName = CommonUtil.getNewFileName(originalFilename); String newSubDirectories = CommonUtil.getNewSubDirectoriesBasedOnNewFileName(newFileName); File filePath = new File(savePathRoot, newSubDirectories); if (!filePath.exists()) { filePath.mkdirs(); } multipartFile.transferTo(new File(filePath + "/" + newFileName)); fileInfo.setOriginalFileName(originalFilename); fileInfo.setNewFileName(newFileName); fileInfo.setSize(multipartFile.getSize()); this.fileList.add(fileInfo); } } session.setAttribute("fileList", this.fileList); session.setAttribute("uploadStatus", "OK"); } else { session.setAttribute("uploadStatus", "ERROR"); } return "redirect:toFileList"; }
这段代码使用了Spring MVC的注解 @RequestMapping,它将该方法映射到了"/upload"的URL路径上。该方法接受了三个参数:MultipartFile[] files、HttpServletRequest request和HttpSession session。其中MultipartFile[] files表示上传的文件,HttpServletRequest request表示当前的请求对象,HttpSession session表示当前的会话对象。
在方法体中,首先检查了上传的文件是否为空,如果不为空则遍历文件数组。在遍历文件数组的过程中,首先创建了一个FileInfo对象,然后获取了上传文件的原始文件名和新文件名,接着根据新文件名创建了一个新的子目录,并将上传文件保存到该目录下。最后,将文件的相关信息存储到FileInfo对象中,并将该对象添加到了一个文件列表中。
最后,将文件列表和上传状态信息存储到会话对象中,并将请求重定向到了"/toFileList"路径。
package com.de.debook.controller; import com.de.debook.bo.ResponseBean; import com.de.debook.constant.WebConstant; import com.de.debook.utils.FileUploadUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.Map; @RestController public class UploadFileController { private static final int FILE_SIZE_MAX = 20 * 1024 * 1024; // 上传限制大小 /** * @param multipartFile * @description: 通用文件上传处理器 * @return: java.util.Map<java.lang.String , java.lang.Object> */ @RequestMapping(value = "/uploadFile", produces = "application/json;charset=UTF-8") public Map<String, Object> fileUpload(@RequestParam("file") MultipartFile multipartFile) { ResponseBean responseBean = new ResponseBean(); if (multipartFile != null) { String realName = multipartFile.getOriginalFilename(); // 原始文件名 String suffix = FileUploadUtils.fileSuffix(realName); // 文件名后缀 String tmpFileName = FileUploadUtils.createTmpFileName(suffix); // 生成保证不重复的临时文件名 if (multipartFile.getSize() > FILE_SIZE_MAX) { responseBean.putError("上传失败:文件大小不得超过20MB"); return responseBean.getResponseMap(); } File tmpFile = new File(WebConstant.FILE_STORAGE_ROOT,tmpFileName); try { multipartFile.transferTo(tmpFile); // 写入本地 responseBean.putData("data", "/images/" + tmpFileName); } catch (IOException e) { responseBean.putError("上传失败:" + e.getMessage()); e.printStackTrace(); } } return responseBean.getResponseMap(); } }
这是一个基于Spring框架开发的文件上传处理器,它接收一个MultipartFile类型的参数,即上传的文件,然后进行处理,最终返回一个包含上传结果的ResponseBean对象。其中,文件大小限制为20MB,超过限制则返回上传失败的信息;上传成功后,会将文件写入到本地,并将其访问路径存储到ResponseBean中返回。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)