java上传文件

public void upload(MultipartFile file, String saveurl,String showurl,) throws Exception {
try {
            String suffix = file.getOriginalFilename().substring(  //文件后缀
            file.getOriginalFilename().lastIndexOf(".") + 1);
            File destFile = new File(saveurl);
            if (!destFile.exists()) {
                destFile.mkdirs();
            }
            String fileNameNew = getFileNameNew() + "." + suffix;//新的文件名
            File f = new File(destFile.getAbsoluteFile() + "/" + fileNameNew);//保存路径
        f.createNewFile();
file.transferTo(f); // fileName = basePath + destDir + fileNameNew;
fileName = saveurl + fileNameNew;
   oldfileName
= file.getOriginalFilename();
       newfileName
= fileNameNew;
}

catch (Exception e) { throw e; }
}

 

posted @ 2017-10-27 10:40  冬天不眠  阅读(308)  评论(0编辑  收藏  举报