1.单文件上传到服务器
index.html
Title
fileController
@RestController
public class fileuploadController {
/**
* 按日期分类
*/
SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
@RequestMapping("/upload")
public String upliad(MultipartFile file, HttpServletRequest req){
//获取临时目录
String realpath=req.getServletContext().getRealPath("/");
//最终路径
String format = sdf.format(new Date());
String path=realpath+ format;
File folder=new File(path);
if (!folder.exists()){
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
try {
file.transferTo(new File(path+newName));
String newName1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
return newName1;
// return path;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
返回结果
打开链接就可以看见上传的文件
2.单文件上传到本地
@RequestMapping("/upload")
public String upload2(@RequestBody MultipartFile file, HttpServletRequest req) throws IOException {//MultipartFile 接收前端传过来的文件
String format = sdf.format(new Date());
String path="D:/img"+format;
File folder=new File(path);
if (!folder.exists()){
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
file.transferTo(new File(path+newName));
return path+ newName;
}
多文件上传
1.一次性选择多个文件
index.html
Title
updaloadController
SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
@RequestMapping("/uploads2")
public String upliad(MultipartFile[] files, HttpServletRequest req){
//获取临时目录
String realpath=req.getServletContext().getRealPath("/");
//最终路径
String format = sdf.format(new Date());
String path=realpath+ format;
File folder=new File(path);
if (!folder.exists()){
folder.mkdirs();
}
try {
for (MultipartFile file:files){
String oldName = file.getOriginalFilename();
String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
file.transferTo(new File(path+newName));
String newName1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
System.out.println(newName1);
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
2.选择多次
index.html
SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
@RequestMapping("/uploads3")
public String upliad(MultipartFile files1,MultipartFile files2, HttpServletRequest req){
//获取临时目录
String realpath=req.getServletContext().getRealPath("/");
//最终路径
String format = sdf.format(new Date());
String path=realpath+ format;
File folder=new File(path);
if (!folder.exists()){
folder.mkdirs();
}
try {
String oldName = files1.getOriginalFilename();
String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
files1.transferTo(new File(path+newName));
String s1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
System.out.println(s1);
String oldName2 = files2.getOriginalFilename();
String newName2= UUID.randomUUID().toString()+oldName2.substring(oldName2.lastIndexOf("."));
files2.transferTo(new File(path+newName2));
String s2 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName2;
System.out.println(s2);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
updaloadController
Title
ajax上传
ajax.html
Title
uploadController
SimpleDateFormat sdf=new SimpleDateFormat("/yyyy/MM/dd/");
@RequestMapping("/upload")
public String upliad(MultipartFile file, HttpServletRequest req){
//获取临时目录
String realpath=req.getServletContext().getRealPath("/");
//最终路径
String format = sdf.format(new Date());
String path=realpath+ format;
File folder=new File(path);
if (!folder.exists()){
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
try {
file.transferTo(new File(path+newName));
String newName1 =req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+req.getContextPath()+format+ newName;
return newName1;
// return path;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)