私は非常に単純なGWTアプリケーションを作成しようとしています。ユーザーはtxtファイルを選択してサーバーにアップロードできます。その後、私はより多くの機能を実装したいが、今の私はのFileUploadにこだわっている:サーブレット付きGWT FileUpload
クライアント側で、私は次のコードの作業があります。私のサーバー側で
public class GwtDemoProject implements EntryPoint {
private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL() + "upload";
private FormPanel form;
private Label info;
private FileUpload fileupload;
private Button uploadFileBtn;
public void onModuleLoad() {
init();
uploadFileBtn.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String filename = fileupload.getFilename();
if(filename.length() == 0) {
Window.alert("File Upload failed");
} else if(filename.endsWith(".txt")) {
form.submit();
} else {
Window.alert("File is not a txt-file");
}
}
});
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
if(event.getResults().length() == 0) {
} else {
Window.alert(event.getResults());
}
}
});
VerticalPanel vp = new VerticalPanel();
vp.add(info);
vp.add(fileupload);
vp.add(new HTML("<br>"));
vp.add(uploadFileBtn);
form.add(vp);
RootPanel rp = RootPanel.get();
rp.add(form);
}
private void init() {
form = new FormPanel();
form.setAction(UPLOAD_ACTION_URL);
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
info = new Label("Wähle eine Textdatei aus");
fileupload = new FileUpload();
uploadFileBtn = new Button("Upload File");
}
}
を私は次のように作られた。
web.xmlにpublic class FileUploadServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
File uploadedFile = new File("C:\\samplePath\\"+item.getName()+".txt");
item.write(uploadedFile);
}
} catch (Exception exc) {
}
}
}
私は、サーブレットに次を追加しました:
<!-- Servlets -->
<servlet>
<servlet-name>uploadServlet</servlet-name>
<servlet-class>de.gwt.demo.server.FileUploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>uploadServlet</servlet-name>
<url-pattern>/gwtdemoproject/upload</url-pattern>
</servlet-mapping>
エラーメッセージは表示されませんが、サーブレットのListは空であるため、whileループは実行されません。リクエストや送信に何か問題がありますか?
質問する前にhttp://stackoverflow.com/questions/1111130/basic-file-upload-in-gwtをチェックしましたか?あなたがしていないと言っているわけではなく、あなたがしたかどうかを知りたいだけです。 – walen
firefox/chromeの開発ツールを使用して、ネットワーク上の状況を教えてください。 http取得リクエストはありますか?そして、もしどのHTTPステータスコードがリクエストの結果であるのであれば? – Akkusativobjekt
@Akkusativobjekt私は何とか私の問題を解決しました - 私はそれを回答として投稿し、変更を強調するつもりです – KilledByCheese