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) {
}
});
}
....
}
我不知道下一步该做什么,
信息:我也试过 this solution,但没用(什么都没发生)。
您不应使用 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());
我正在做一个项目,我下载了一个在服务器中生成的文件, 我在服务器中使用 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,但没用(什么都没发生)。
您不应使用 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());