19. 文件上传下载

19. 文件上传下载

上传
@RequestMapping("/upload")
    public void ss(MultipartFile file){
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
            File upload = new File(path.getAbsolutePath(),"static/img/");
            if(!upload.exists()){
                upload.mkdirs();
            }
            File fileUp = new File(upload.getAbsolutePath()+ "/"+file.getOriginalFilename());
            file.transferTo(fileUp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
下载
@RequestMapping(path = "/download", method = RequestMethod.GET)
public ResponseEntity<Resource> download(String param) throws IOException {

    // ...

    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

    return ResponseEntity.ok()
            .headers(headers)
            .contentLength(file.length())
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .body(resource);
}
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SAP系统中的文件上传下载可以通过SAP内置的函数模块 `GUI_UPLOAD` 和 `GUI_DOWNLOAD` 来实现。下面是一个SAP文件上传下载的示例: 文件上传: ``` REPORT z_upload_file. DATA: lv_file TYPE string VALUE 'C:\temp\test.txt', lt_data TYPE STANDARD TABLE OF soli WITH HEADER LINE. * 读取本地文件数据 CALL FUNCTION 'GUI_UPLOAD' EXPORTING filename = lv_file TABLES data_tab = lt_data EXCEPTIONS file_open_error = 1 file_read_error = 2 no_batch = 3 gui_refuse_filetransfer = 4 invalid_type = 5 no_authority = 6 unknown_error = 7 header_not_allowed = 8 separator_not_allowed = 9 filesize_not_allowed = 10 header_too_long = 11 dp_error_create = 12 dp_error_send = 13 dp_error_write = 14 unknown_dp_error = 15 access_denied = 16 dp_out_of_memory = 17 disk_full = 18 dp_timeout = 19 file_not_found = 20 dataprovider_exception = 21 control_flush_error = 22 OTHERS = 23. IF sy-subrc EQ 0. MESSAGE '文件上传成功' TYPE 'S'. ELSE. MESSAGE '文件上传失败' TYPE 'E'. ENDIF. ``` 文件下载: ``` REPORT z_download_file. DATA: lv_file TYPE string VALUE 'C:\temp\test.txt', lt_data TYPE STANDARD TABLE OF soli WITH HEADER LINE. * 读取服务器文件数据 CALL FUNCTION 'GUI_DOWNLOAD' EXPORTING filename = lv_file TABLES data_tab = lt_data EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 no_authority = 5 unknown_error = 6 header_not_allowed = 7 separator_not_allowed = 8 filesize_not_allowed = 9 header_too_long = 10 dp_error_create = 11 dp_error_send = 12 dp_error_write = 13 unknown_dp_error = 14 access_denied = 15 dp_out_of_memory = 16 disk_full = 17 dp_timeout = 18 file_not_found = 19 dataprovider_exception = 20 control_flush_error = 21 OTHERS = 22. IF sy-subrc EQ 0. MESSAGE '文件下载成功' TYPE 'S'. ELSE. MESSAGE '文件下载失败' TYPE 'E'. ENDIF. ``` 请根据实际需要,修改文件路径和文件名等参数。同时,需要注意的是,这种方式只适用于小文件的上传和下载。对于大文件,需要采用其他的方式,例如使用SAP数据传输工具(SAP Data Transfer Workbench)或者FTP等方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我超爱写bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值