0


java实现文件的上传和下载,将文件流转base64返回给前端

上传代码

publicResultInfo<?>uploadFile(@RequestParamMultipartFile file,@RequestParamString id)throwsBusinessException{try{if(file.isEmpty()){returnJsonResult.error(StatusCode.ERROR_ADD);}// 获取文件名String fileName = file.getOriginalFilename();System.out.println("上传的文件名为:"+ fileName);String preName = fileName.substring(0,fileName.lastIndexOf("."));// 获取文件的后缀名String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);System.out.println("文件的后缀名为:"+ suffixName);// 设置文件存储路径         *************************************************String filePath ="自己的文件路径/";String path = filePath + fileName;File dest =newFile(newFile(path).getAbsolutePath());// dist为文件,有多级目录的文件// 检测是否存在目录if(dest.getParentFile().exists()){//因此这里使用.getParentFile(),目的就是取文件前面目录的路径//如果存在文件夹FileSystemUtils.deleteRecursively(dest.getParentFile());// 删除文件夹}
            dest.getParentFile().mkdirs();// 新建文件夹
            file.transferTo(dest);// 文件写入returnthis.update(actionData);}catch(IllegalStateException e){
            e.printStackTrace();thrownewBusinessException("999999","业务数据操作异常", e);}catch(IOException e){
            e.printStackTrace();thrownewBusinessException("999999","业务数据操作异常", e);}}

文件下载代码

publicResultInfo<?>downloadFile(String fileName ,HttpServletResponse response)throwsBusinessException{if(fileName !=null){//设置文件路径String filePath ="自己的文件路径"+"/"+fileName;File file =newFile(filePath);if(file.exists()){
                response.addHeader("Content-Disposition","attachment;fileName="+ fileName);// 设置文件名byte[] buffer =newbyte[1024];FileInputStream fis =null;BufferedInputStream bis =null;try{
                    fis =newFileInputStream(file);
                    bis =newBufferedInputStream(fis);OutputStream os = response.getOutputStream();int i = bis.read(buffer);while(i !=-1){
                        os.write(buffer,0, i);
                        i = bis.read(buffer);}returnJsonResult.success(bis);}catch(Exception e){
                    e.printStackTrace();}finally{// 做关闭操作if(bis !=null){try{
                            bis.close();}catch(IOException e){
                            e.printStackTrace();}}if(fis !=null){try{
                            fis.close();}catch(IOException e){
                            e.printStackTrace();}}}}}returnJsonResult.error(StatusCode.ERROR_ADD);}

后端通过json拿到文件流的返回,我采用将文件流转成base64返回给前台,实现代码如下:

publicResultInfo<?>downloadFile(String fileName )throwsBusinessException{if(fileName !=null){//设置文件路径String filePath ="自己的文件路径/"+fileName;String base64 =null;InputStream in =null;try{File file =newFile(filePath);
                in =newFileInputStream(file);byte[] bytes=newbyte[(int)file.length()];
                in.read(bytes);
                base64 =Base64.getEncoder().encodeToString(bytes);}catch(Exception e){
                e.printStackTrace();thrownewBusinessException("999999","业务数据操作异常", e);}finally{if(in!=null){try{
                        in.close();}catch(IOException e){
                        e.printStackTrace();thrownewBusinessException("999999","业务数据操作异常", e);}}}returnJsonResult.success(base64);}returnJsonResult.error(StatusCode.ERROR_ADD);}
标签: java 前端 spring boot

本文转载自: https://blog.csdn.net/weixin_44812604/article/details/128252709
版权归原作者 小白一枚0124 所有, 如有侵权,请联系我们删除。

“java实现文件的上传和下载,将文件流转base64返回给前端”的评论:

还没有评论