一直受限于用企鹅在电脑和手机之间传文件

避免不了要在其他端登录而造成一定的风险性

就自己做了一个文件传输系统

实现

  • 后台:Spring Boot
  • 数据库:Mysql
  • 前端:React
  • Android:Java

工具

  • Intellij IDEA 2020.1.2
  • WebStorm 2020.1.2
  • Android Studio
  • Microsoft Edge
  • Android 手机

Api

  • 上传:/upload POST
  • 下载:/download GET
  • 预览:/preview GET
  • 列表:/list GET
@RestController
@RequestMapping("/file")
@AllArgsConstructor
public class FileAdminApi {

    @Autowired
    private IFileService fileService;

    @PostMapping("/upload")
    public ResponseResult uploadFile(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        return fileService.uploadFile(response, file);
    }

    @GetMapping("/preview/{fileId}")
    public ResponseResult previewFile(HttpServletResponse response, HttpServletRequest request, @PathVariable("fileId") String fileId) {
        return fileService.previewFile(response, request, fileId);
    }
    @GetMapping("/download/{fileId}")
    public ResponseResult downloadFile(HttpServletResponse response, @PathVariable("fileId") String fileId) {
        return fileService.downloadFile(response, fileId);
    }
    @GetMapping("/list")
    public ResponseResult listFile() {
        return fileService.listFile();
    }

}
java

部分Api实现

	@Override
    public ResponseResult uploadFile(HttpServletResponse response, MultipartFile file) {
        if (file == null) {
            return ResponseResult.FAILED(CONTENT_NULL);
        }
        String dateStr = simpleDateFormat.format(new Date());
        String datePath = ROOT + File.separator + dateStr;
        File datePathDir = new File(datePath);
        if (!datePathDir.exists()) {
            datePathDir.mkdirs();
        }
        String contentType = file.getContentType();
        log.info(TAG, contentType);
        if (TextUtils.isEmpty(contentType)) {
            return ResponseResult.FAILED("文件类型不支持!");
        }
        contentType = contentType.split("/")[1];
        String typePath = datePath + File.separator + contentType;
        File typePathDir = new File(typePath);
        if (!typePathDir.getParentFile().exists()) {
            typePathDir.mkdirs();
        }
        String originalFilename = file.getOriginalFilename();
        String path = typePath + File.separator + originalFilename;
        File target = new File(path);
        if (!target.getParentFile().exists()) {
            target.getParentFile().mkdirs();
        }
        try {
            if (!target.exists()) {
                target.createNewFile();
            }
            file.transferTo(target);
            cool.hyz.blog.pojo.File fileDb = new cool.hyz.blog.pojo.File();
            fileDb.setId(idWorker.nextId() + "");
            fileDb.setName(originalFilename);
            fileDb.setType(contentType);
            fileDb.setPath(path);
            fileDb.setCreateTime(new Date());
            fileDb.setUpdateTime(new Date());
            fileDao.save(fileDb);
            return ResponseResult.SUCCESS("上传成功!").setData(fileDb);

        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.FAILED(e.getMessage());
        }
    }


    @Override
    public ResponseResult downloadFile(HttpServletResponse response, String fileId) {
        if (TextUtils.isEmpty(fileId)) {
            return ResponseResult.FAILED(CONTENT_NULL);
        }
        cool.hyz.blog.pojo.File file = fileDao.findOneById(fileId);
        String path = file.getPath();
        if (TextUtils.isEmpty(path)) {
            return ResponseResult.FAILED("找不到文件路径");
        }
        try {
            byte[] buff = new byte[1024];
            response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
            FileInputStream fis = null;
            OutputStream os = null;
            fis = new FileInputStream(path);
            int size = fis.available(); // 得到文件大小
            byte data[] = new byte[size];
            fis.read(data); // 读数据
            fis.close();
            response.setContentType(file.getType()); // 设置返回的文件类型
            os = response.getOutputStream();
            os.write(data);
            os.flush();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return ResponseResult.FAILED(GET_FAILED);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
java

Android下载

 	@Override
    public void ondFileClickToDownload(int position, GetDownloadList.DataBean dataBean) {
        String url = DOWNLOAD_FILE + dataBean.getId();//下载链接(api)
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setAllowedOverRoaming(false);
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setTitle(dataBean.getName());
        request.setDescription("文件正在下载中......");
        request.setVisibleInDownloadsUi(true);
        //设置下载的路径
        File file = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), dataBean.getName());
        request.setDestinationUri(Uri.fromFile(file));
        String path = file.getAbsolutePath();

        //获取DownloadManager
        if (downloadManager == null) {
            downloadManager = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE);
        }
        //将下载请求加入下载队列
        if (downloadManager != null) {
            downloadId = downloadManager.enqueue(request);
        }
    }
java

UI就不贴出来了,写得很简陋

就先到这里啦

2021-03-23 Web Socket +Rtc 实现视频通话(二)

2021-03-22 对于实现视频通话(一)的补充

2021-03-21 Web Socket +Rtc 实现视频通话(一)

2021-02-03 Vuex实现组件间通讯

2021-02-03 Ansys 计算实例

2020.12.19 今日份日记

2020.12.13 docker 介绍及常见命令

2020.12.12 Swagger安全配置——用户名密码

2020.12.12 solr安全配置——用户名密码

2020.11.15 Spring Boot项目创建

2020.11.1 JavaScript网络爬虫之——英文文章

2020.9.15 今日份日记

2020.9.01 今日份日记

2020.8.07 今日份日记

2020.8.02 晚安

2020.7.31 今日份日记

2020.7.31 你还要我怎样

2020.7.24 乐谱收藏夹(一)

2020.7.24 下雨了

2020.7.21 米瑶留言小程序上线啦~

2020.7.21 微信小程序开发之——米瑶云音乐

2020.7.14 网站分享

2020.7.08 技术分享——批量修改文件名

2020.6.11 访寻

2020.5.21 来世

欢迎关注我的微信公众号

续加仪

打赏
  • 微信
  • 支付宝
评论
来发评论吧~
···

歌手: