文件上传java接口

@PostMapping("/uploafile")
    public Response uploafile(MultipartHttpServletRequest request) {
        MultipartFile uploadFile = request.getFile("icingExcel");
        if (uploadFile == null) {
            return Response.fail("需要上传文件!");
        }

        String originalFilename = uploadFile.getOriginalFilename();
        int length = originalFilename.length();
        int i = originalFilename.lastIndexOf(".xlsx");
        if (length < 6 || i == -1 || i != length - 5) {
            return Response.fail("文件格式不正确");
        }
				
				//文件下载地址
        String filePath = "F:/testUploadDir".concat("/").concat(originalFilename);

        if (filePath == null) {
            return Response.fail("文件类型不合法");
        }
        File file = new File(filePath);
        if (file.exists()) {
            System.out.println("文件存在,将其删除");
            file.delete();
        }
        FileOutputStream fos = null;
        InputStream inputStream = null;
        try {
            fos = new FileOutputStream(file);
            inputStream = uploadFile.getInputStream();
            byte[] bytes = new byte[1024];
            while (inputStream.read(bytes) != -1) {
                fos.write(bytes);
            }
            System.out.println("文件写入结束!");
            return Response.success();
        } catch (Exception ex) {
            System.out.println("上传覆冰设备状态统计模板时出错");
            System.out.println(ex.getMessage());
            return Response.fail("文件上传失败!");
        }

    }


你可能感兴趣的:(java,开发语言)