文章标题

public class upLoad {
public static void getUpload(List mlist,HttpServletRequest req,String newsuppId,User u ){
String path=req.getServletContext().getRealPath(“/actter”);//得到服务器端地址
int i1=0;
for(MultipartFile mult:mlist){//MultipartFile自带解析方法
String oldeName=mult.getOriginalFilename();//得到老名字
if(oldeName!=null&&!”“.equals(oldeName)){
//修改后的名字=毫秒值+原来的老名字
String newName=System.currentTimeMillis()+oldeName;//新名字,避免名字重复所以取时分秒
int xiabiao=oldeName.lastIndexOf(“.”);//截取字符串
//得到后缀(三元运算符也可以分开写)
String suffix=xiabiao>0?oldeName.substring(xiabiao+1):”“;
// if(xia>0){
// suffix=file.substring(xia+1);
// }else {
// suffix=”“;
// }
String realPath=path+”actter/”+getRiQi()+newName;
String newRealPath=path+”actter/”+getRiQi();
File f1=new File(newRealPath);
f1.getParentFile().mkdirs();//创建文件夹(多个上传)
// f1.getParentFile().mkdir();单个文件夹

             Actter ac=new Actter();
             ac.setOldname(oldeName);
             ac.setSavename(newName);
             ac.setAdddate(new Date());
             ac.setSavepath(newRealPath);
            try {
                InputStream is=mult.getInputStream();
                OutputStream os=new FileOutputStream(newRealPath);
                byte[] bys=new byte[2048];
                int len=0;
                while((len=is.read(bys))!=-1){
                    os.write(bys, 0,len);
                }
                os.close();
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         }

    }

}
public static String getRiQi(){
    Calendar calendar=Calendar.getInstance();
    StringBuffer sb=new StringBuffer();
    sb.append(calendar.get(Calendar.YEAR)).append("/");
    sb.append(calendar.get(Calendar.MONTH)+1).append("/");
    sb.append(calendar.get(Calendar.DATE)).append("/");
    return sb.toString();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值