图片及视频上传

public static String fileUpLoad(MultipartFile file,String url){
if(file!=null&&!file.getOriginalFilename().equals("")){
Calendar date = Calendar.getInstance();
    int day = date.get(Calendar.DAY_OF_MONTH);
    int month = date.get(date.MONTH);
    int year = date.get(date.YEAR);
    long timeStamp = CalendarUtils.getTimeStamp();
    String str = year+""+month+"/"+day;
File file1 = new File(SystemConstants.BASE_URL+str+"/"+url);
if(!file1.exists()){
file1.mkdirs();
}
String name = file.getOriginalFilename();
String[] split = name.split("\\.");
String string = split[split.length-1];
String fileName = timeStamp+"."+string;
File file2 = new File(file1,fileName);
if(!file2.exists()){
try {
file.transferTo(file2);
} catch (Exception e) {
e.printStackTrace();
}
}
return SystemConstants.REDIRECT_URL+str+"/"+url+fileName;
}else{
return null;
}
}
/**
* 更新文件
* @param file
* @param oldUrl
* @param newUrl
*/
public static String fileUpdate(MultipartFile file,String oldUrl,String newUrl){
if(file!=null){
if(oldUrl!=null&&!oldUrl.equals("")){
String deleteUrl = oldUrl.replace(SystemConstants.REDIRECT_URL,"");
File file1 = new File(SystemConstants.BASE_URL+deleteUrl);
//System.out.println(SystemConstants.BASE_URL+deleteUrl);
file1.delete();
/*try {
Process process = Runtime.getRuntime().exec("rm -f "+deleteUrl);
System.out.println("删除成功");
} catch (IOException e) {
System.out.println("删除文件失败");
e.printStackTrace();
}*/
}
return fileUpLoad(file, newUrl);
}else{
return null;
}
}
/**
* 删除文件
* @param request
* @param url
*/
public static void deleteOnExit(String url){
if(url!=null){
String deleteUrl = url.replace(SystemConstants.REDIRECT_URL,"");
File file1 = new File(SystemConstants.BASE_URL+deleteUrl);
//System.out.println(SystemConstants.BASE_URL+deleteUrl);
file1.delete();
/*try {
Process process = Runtime.getRuntime().exec("rm -f "+deleteUrl);
System.out.println("删除成功");
} catch (IOException e) {
System.out.println("删除文件失败");
e.printStackTrace();
}*/
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值