GWTP和Spring REST文件下载

时间:2015-05-06 14:11:10

标签: java spring rest download gwtp

我正在开发一个项目,我已经下载了服务器中生成的文件, 我在服务器中使用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) {
                    }
                });
    }

    ....
}

我不知道接下来要做什么,

信息:我也尝试了this solution,但它没有用(没有任何反应)。

1 个答案:

答案 0 :(得分:0)

您不应该使用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());