我正在使用GWTUpload上傳文件。 我在onFinishHandler中獲取服務器信息,文件名,內容類型等等,但是沒有選擇從服務器到客戶端獲取文件內容。如何使用gwtupload從文件中獲取文件內容
注:我試圖上傳XML文件和Excel文件
我使用GWT 2.4,GXT 3.0.1,GWTUpload 0.6.6
這裏的示例代碼
客戶端代碼 - OnFinishHandler
u.addOnFinishUploadHandler(new OnFinishUploaderHandler() {
@Override
public void onFinish(IUploader uploader) {
if (uploader.getStatus() == Status.SUCCESS) {
System.err.println(uploader.getServerResponse());
UploadedInfo info = uploader.getServerInfo();
System.out.println("File name " + info.name);
System.out.println("File content-type " + info.ctype);
System.out.println("File size " + info.size);
System.out.println("Server message " + info.message);
}
}
});
servlet代碼
public class GWTFileUploadServlet extends UploadAction {
private static final long serialVersionUID = -6742854283091447922L;
String fileContent;
File uploadedFile;
@Override
public String executeAction(HttpServletRequest request,
List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
int cont = 0;
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
cont++;
try {
File file = File.createTempFile("upload-", ".bin");
item.write(file);
uploadedFile = file;
fileContent = item.getContentType();
response += "File saved as " + file.getAbsolutePath();
} catch (Exception e) {
throw new UploadActionException(e.getMessage());
}
}
}
removeSessionFileItems(request);
return response;
}
@Override
public void getUploadedFile(HttpServletRequest request,
HttpServletResponse response) throws IOException {
if (fileContent != null && !fileContent.isEmpty()) {
response.setContentType(fileContent);
FileInputStream is = new FileInputStream(uploadedFile);
copyFromInputStreamToOutputStream(is, response.getOutputStream());
} else {
renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND);
}
}
}
請幫我....
GwtUpload內部使用IOUtils.copy將輸入流複製到輸出流。我的問題是如何獲取正在複製到outputstream的文件內容。 copyFromInputStreamToOutputStream(is,response.getOutputStream()); 我想在OnFinishHandler裏面的迴應 – Lakshminarayana
你是說你想在客戶端讀取內容嗎? –
是的,我想要的文件內容在客戶端 – Lakshminarayana