我在我的代码中使用 GWt 文件上传器。在服务器端,我有 getContentType 来获取 MIME 类型。当我在本地运行它并上传 .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);