springboot多文件上传、删除、下载到项目本地

@RequestMapping("uploading")
    public String uploading(MultipartFile[] file, HttpServletRequest request){
        if(file != null && file.length>0){
            String address="";
            try {
                for (int j = 0; j < file.length; j++) {
                    MultipartFile multipartFile=file[j];
                    //获取本地文件存放路径
                    String dir = request.getSession().getServletContext().getRealPath("/")+"upload/";
                    //获取文件名
                    String fileName = multipartFile.getOriginalFilename();
                    //设置新文件名称
                    String newfileName = IdGen.uuid()+fileName.substring(fileName.lastIndexOf("."));
                    //上传文件
                    multipartFile.transferTo(new File(dir + newfileName));
                    String url="upload/"+newfileName;
                    if(j!=file.length-1){
                        address+=url+",";
                    }else{
                        address+=url;
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
            return address;
        }
        return "上传失败";
    }



 /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true,否则返回false
     */
    @SuppressWarnings("unused")
    public static boolean deleteFile(String sPath, HttpServletRequest request) {
        boolean flag = false;
        String dir = request.getSession().getServletContext().getRealPath("/");
        sPath = dir.replace("\\", "/")+sPath;
        File  file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }




//下载文件 传入HttpServletResponse对象response 和 文件保存位置path
public static void downloadFile(HttpServletResponse response, String path) {
    try {
        response.setCharacterEncoding("UTF-8");
        File file = new File(path);
        //如果文件不存在
        if (file == null || !file.exists()) {
            ResponseState responseState = new ResponseState();
            responseState.setMsg("文件不存在!");
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(responseState));
            out.flush();
            out.close();
        }
        String simpleName = file.getName().substring(file.getName().lastIndexOf("/") + 1);
        String newFileName = new String(simpleName.getBytes(), "utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + newFileName);
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(
                response.getOutputStream());
        byte[] buffer = new byte[1024];
        int length;
        while ((length = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        if (bis != null){
            bis.close();
        }
        if (bos != null){
            bos.close();
        }
    } catch (Exception e) {
        // TODO: handle exception
    }
}

你可能感兴趣的:(springboot)