java中的接口是类吗
335
2022-07-25
目录请求类型单文件上传多文件上传多文件单字段上传多文件多字段上传
请求类型
单文件上传
//上传单个文件
@ResponseBody
@RequestMapping(value = "uploadfile", produces = "multipart/form-data;charset=UTF-8")
public String uploadfiles(HttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("file");
if (multipartFile == null) {
System.out.println("multipartFile == null");
}
String filename = multipartRequest.getFile("file").getOriginalFilename();
String tmpFile = request.getSession().getServletContext().getRealPath("/") + UUID.randomUUID().toString().replace("-", "") + ".apk";
File targetFile = new File(tmpFile);
AppEntity appEntity = new AppEntity();
if (null != multipartFile) {
//保存文件
try {
multipartFile.transferTo(targetFile);
} catch (IllegalStateException e) {
logger.error("multipartFile.transferTo, IllegalStateException", e);
} catch (IOException e) {
logger.error("multipartFile.transferTo, IOException", e);
}
}
}
return "";
}
多文件上传
多文件单字段上传
@ResponseBody
@RequestMapping(value = "uploadfiles", produces = "multipart/form-data;charset=UTF-8")
public String uploadfile(MultipartHttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
//上传多个文件,一个字段中包含多个文件
String projectPath = request.getSession().getServletContext().getRealPath("/");
MultiValueMap
List
for(Map.Entry
fileSet = temp.getValue();
}
for(MultipartFile temp : fileSet){
File file2 = new File(projectPath+temp.getOriginalFilename());
try {
temp.transferTo(file2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
多文件多字段上传
@ResponseBody
@RequestMapping(value = "uploadfiles", produces = "multipart/form-data;charset=UTF-8")
public String uploadfile(MultipartHttpServletRequest request){
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
//上传多个文件,每个字段一个文件
Iterator
while (fileNames.hasNext()){
// 取得上传文件
String uploadName = fileNames.next();
MultipartFile file = request.getFile(uploadName);
if (file != null) {
String projectPath = request.getSession().getServletContext().getRealPath("/");
String originalFilename = file.getOriginalFilename();
String tmpFile = projectPath + System.currentTimeMillis() + "_" + originalFilename;
File targetFile = new File(tmpFile);
try {
file.transferTo(targetFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
jzsUxw }
return "";
}
如果上面方法不行,看下面
@PostMapping(value = "test", produces = "multipart/form-data;charset=UTF-8")
public void UploadFiles(HttpServletRequest request) throws IOException {
//如果请求中包含文件
if(ServletFileUpload.isMultipartContent(request)){
// 创建一个通用的多部分解析器
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
List
File targetFile;
for (MultipartFile file : files){
//保存文件到本地
String projectPath = request.getSession().getServletContext().getRealPath("/");
projectPath = projectPath + System.currentTimeMillis() + "_" + new Random().nextInt(100);
File dir = new File(projectPath);
if (!dir.exists()){
dir.mkdir();
}
String originalFilename = file.getOriginalFilename();
String tmpFile = projectPath + File.separator + originalFilename;
targetFile = new File(tmpFile);
file.transferTo(targetFile);
//将文件保存到本地并记录信息
//...
}
}
}
还有一种方法,函数入参直接接受文件
例如:
//单文件
public String UploadFiles(@RequestParam("file") MultipartFile file){
}
//多文件
public String UploadFiles(@RequestParam("file") List
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~