簡體   English   中英

GWTP和Spring REST文件下載

[英]GWTP and Spring REST file download

我正在一個項目中,我已經下載了服務器中生成的文件,我正在服務器中使用Spring:

@RestController
@RequestMapping(value = ApiPaths.FORM)
public class ExportController {
Log log = LogFactory.getLog(getClass());
@Autowired
DataExportService dataExportService;

@RequestMapping(method = GET, value = PathParameter.ID, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ResponseBody public HttpEntity<byte[]> getFile(@PathVariable(RestParameter.ID) Long id) {
    FileSystemResource file = null;
    try {
        file = dataExportService.exportData("file", id);
        if (file.exists()) {
            byte[] bytes = IOUtils.toByteArray(file.getInputStream());
            HttpHeaders header = new HttpHeaders();
            header.set("Content-Disposition", "attachment; filename=" + file.getFilename());
            header.setContentLength(file.getFile().length());
            return new HttpEntity<>(bytes, header);
        }
    } catch (IOException e) {
        log.info(e.getMessage());
        e.printStackTrace();
    }
    return new HttpEntity<>(null, new HttpHeaders());
}

}

該文件生成的很好,但是在客戶端,我不知道如何繼續,我嘗試了這個:

我有這項服務:

@Path(ApiPaths.FORM)
public interface ExportFormService {
     @GET
     @Path(PathParameter.ID)
     RestAction<byte[]> exportformById(@PathParam(RestParameter.ID) Long id);
}

在小部件的演示者中使用(單擊按鈕時):

public class DynamicFormsPresenter extends Presenter<MyView, MyProxy> implements DynamicFormsUiHandlers,
        FormSavedEvent.FormSavedHandler {

    @ProxyStandard
    @NameToken({NameTokens.FORM, NameTokens.FORM_DETAILS})
    public interface MyProxy extends ProxyPlace<DynamicFormsPresenter> {
    }

    public interface MyView extends View, HasUiHandlers<DynamicFormsUiHandlers> {
        ...
    }

    public static final Object FORM_BUILDER = new Object();

    private final PlaceManager placeManager;
    private final RestDispatch dispatcher;
    private final PlaceRequest placeRequest;
    private final ExportFormService exportFormService;
    private final FormBuilderPresenterFactory formBuilderPresenterFactory;

    @Inject
    DynamicFormsPresenter(EventBus eventBus,
                          MyView view,
                          MyProxy proxy,
                          PlaceManager placeManager,
                          RestDispatch dispatcher,
                          PlaceRequest placeRequest,
                          DynamicFormService dynamicFormService,
                          ExportFormService exportFormService,
                          FormBuilderPresenterFactory formBuilderPresenterFactory) {
        super(eventBus, view, proxy, EntryPresenter.SLOT_ENTRY);

        this.placeManager = placeManager;
        this.dispatcher = dispatcher;
        this.placeRequest = placeRequest;
        this.exportFormService = exportFormService;
        this.formBuilderPresenterFactory = formBuilderPresenterFactory;

    }

    ....

    @Override
    public void exportForm(DynamicFormVO dynamicFormVO) {
        dispatcher.execute(exportFormService.exportformById(dynamicFormVO.getId()),
                new AbstractAsyncCallback<byte[]>() {
                    @Override
                    public void onReceive(byte[] response) {
                    }
                });
    }

    ....
}

我不知道接下來要做什么

信息:我也嘗試了此解決方案 ,但是沒有用(什么都沒發生)。

您不應該使用GWTP的REST Dispatch進行文件下載。 只需在視圖中添加框架

<g:Frame ui:field="downloadFrame" height="0" width="0" visible="false"/>

然后將框架的URL設置為Spring處理程序的路徑:

UrlBuilder builder = new UrlBuilder()
            .setProtocol(Window.Location.getProtocol())
            .setHost(Window.Location.getHost())
            .setPath(ApiPaths.FORM + id);
downloadFrame.setUrl(builder.build());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM