クライアントが自分のコンピューターでtxtファイルを検索し、それをインポートするようにします。このサイトの設定を含むファイルです。FileUploadを使用して実行しましたが、エラーが発生します。
javax.servlet.ServletException: Content-Type was 'multipart/form-data; boundary=---------------------------14895149261880284533914609527'. Expected 'text/x-gwt-rpc'.
私はどこでも検索しましたが、何もありません....インポートを行う他の方法はありますか?または私が間違っていること。これが私のコードです:
final DialogBox dialogBox = new DialogBox();
dialogBox.setAnimationEnabled(true);
dialogBox.setVisible(true);
dialogBox.center();
dialogBox.setText("Load settings file");
final FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
form.addStyleName("loadSettingsfile");
form.setWidth("275px");
form.setAction(GWT.getModuleBaseURL()+"fileUpload");
VerticalPanel holder = new VerticalPanel();
final FileUpload upload = new FileUpload();
upload.setName("upload");
holder.add(upload);
holder.add(new HTML("<hr />"));
holder.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT);
HorizontalPanel hrPan = new HorizontalPanel();
Button submitButton = new Button("Load settings");
Button cancelButton = new Button("Cancel");
hrPan.add(cancelButton);
hrPan.add(submitButton);
holder.add(hrPan);
form.add(holder);
submitButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
form.submit();
}
});
cancelButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
dialogBox.hide();
}
});
form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
//Window.alert(event.getResults());
dialogBox.hide();
}
});
form.addSubmitHandler(new SubmitHandler() {
public void onSubmit(SubmitEvent event) {
}
});
dialogBox.setWidget(form);
およびサーブレット:
public class FileUpload extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
response.setContentType("text/x-gwt-rpc");
response.addHeader("Path", request.getPathInfo());
try{
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
int maxFileSize = 10*(1024*1024); //10 megs max
if (out.size() > maxFileSize) {
throw new RuntimeException("File is > than " + maxFileSize);
}
}
}
catch(Exception e){
throw new RuntimeException(e);
}
}