コードで GWt fileuploader を使用しています。サーバー側では、MIME タイプを取得する getContentType があります。ローカルで実行して .PDF ファイルをアップロードすると、ContentType が「application/pdf」として返されます。しかし、アプリケーション サーバーにコードをデプロイすると、ContentType が "application/octet-stream" として返されます。
MIME タイプを設定する必要があります。ここでは、ContentType を MIMEtype に設定しています。別の ContentType を取得すると、「application/octet-stream」の呼び出しが失敗します。
以下はコードです:
Iterator<FileItem> iter = items.iterator();
byte[] file ;
byte[] apacheBytes;
System.out.println("before while" );
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
fileName = item.getName();
System.out.println("fileName is : " + fileName);
typeMime = item.getContentType();
System.out.println("typeMime is : " + typeMime);
int sizeInBytes = (int) item.getSize();
System.out.println("Size in bytes is : " + sizeInBytes);
file = item.get();
apacheBytes = org.apache.commons.codec.binary.Base64.encodeBase64(file);
System.out.println("The ApacheBytes="+apacheBytes);
}
クライアント側コード:
FlowPanel mainPanel=new FlowPanel();
form = new FormPanel();
form.setMethod(FormPanel.METHOD_POST);
//The HTTP request is encoded in multipart format.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
// multipart MIME encoding
form.setAction("/FileUploadServlet");
form.setWidget(mainPanel);
uploadButton=new NFUMButton("Browse");
addButton=new Button("Add");
FileUpload fileUpload = new FileUpload();
fileUpload.setName("uploader");
FlowPanel errPanel=new FlowPanel();
errPanel.setStyleName("error_message");
FlowPanel errIconPanel=new FlowPanel();
Element span=DOM.createElement("span");
span.setInnerText("Document size should not excedd 100mb");
errIconPanel.getElement().insertFirst(span);
errIconPanel.setStyleName("error_icon");
errPanel.add(errIconPanel);
mainPanel.add(errPanel);
FlowPanel fileuploadPanel=new FlowPanel();
fileuploadPanel.setStyleName("fileupload");
Element span1=DOM.createElement("span");
//span.setInnerText("(Chetan Document size should not excedd 100mb)");
// fileuploadPanel.getElement().insertAfter(span1, span);
FlowPanel buttonPanel=new FlowPanel();
buttonPanel.setStyleName("button_body");
addButton.setStyleName("add");
buttonPanel.add(addButton);
fileuploadPanel.add(fileUpload);
fileuploadPanel.add(buttonPanel);
mainPanel.add(fileuploadPanel);
//this.initWidget(mainPanel);
this.initWidget(form);