2
這是一個奇怪的。我需要使用restlet api
來下載文件。我正在創建GET
方法中的文件並返回FileRepresentation
。下載工作正常,但文件名始終來自ApplicationRoot
中指定的操作名稱。FileRepresentation返回名稱來自router.attach的文件
這是我ApplicationRoot
相關線路:
router.attach("/LogDownload",LogDataDownload.class);
及相關方法LogDataDownload
:
@Get
public Representation run() {
APIResponseWrapper wrapper = new APIResponseWrapper(null, true);
try{
String fileLogPath = Config.getLogFilePath();
File logFile = new File(fileLogPath);
return FileUtility.createRepForFile(logFile);
}catch(Exception e){
wrapper.setErrorStackTrace(e);
}
return new JacksonRepresentation<APIResponseWrapper>(wrapper);
}
這是我如何創建FileRepresentation
:
public static FileRepresentation createRepForFile(File file) {
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeTypeStr = mimeTypesMap.getContentType(file);
MediaType mt = new MediaType(mimeTypeStr);
FileRepresentation fr = new FileRepresentation(file, mt);
fr.setDisposition(new Disposition(Disposition.TYPE_ATTACHMENT));
return fr;
}
下載通過這個u沒有問題RL:http://server:port/AppName/LogDownload
,但下載的文件名總是LoadDownload
(或不管我放在router.attach
線),我不能將其重命名爲我想要什麼,在這種情況下會logFile.txt
。