本文記錄使用Java API實作上傳的測試心得
上網找到的取得
HttpServletRequest 都不適用於alfresco 3.4.5版本
// NOTE: This web script must be executed in a HTTP Servlet environment
// if (!(req instanceof WebScriptServletRequest)) {
// throw new WebScriptException(
// "Content retrieval must be executed in HTTP Servlet environment");
// }
//
HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
發生錯誤如下: ClassCastException
protected Map<string, object> executeImpl(
WebScriptRequest req,
Status status,
Cache cache) {
WrappingWebScriptRequest wrappingWebScriptRequest = (WrappingWebScriptRequest) req;
WebScriptRequest webScriptRequest = wrappingWebScriptRequest.getNext();
WebScriptServletRequest servletRequest = (WebScriptServletRequest) webScriptRequest;
FormField uploadFile = servletRequest.getFileField("file");
//file field
uploadFileName = uploadFile.getFilename();
uploadMIMEType = uploadFile.getMimetype();
uploadContent = uploadFile.getInputStream();
System.out.println("[form data] filename:" + uploadFileName);
System.out.println("[form data] mimetype:" + uploadMIMEType);
//do something
}
用以下這段程式就能正常取得上傳檔案了,先前的中文問題出在client指定錯誤編碼了,繞了一大圈竟然是手誤呀!!
//how to get WebScriptServletRequest
WrappingWebScriptRequest wrappingWebScriptRequest = (WrappingWebScriptRequest) req;
WebScriptRequest webScriptRequest = wrappingWebScriptRequest.getNext();
WebScriptServletRequest servletRequest = (WebScriptServletRequest) webScriptRequest;
//get data form form
FormData formData = (FormData)servletRequest.parseContent();
FormData.FormField[] formFields = formData.getFields();
int fieldsLen = formFields.length;
for(int i=0;i
如果要做更複雜的行為不在這篇的討論範例 :)