Hello guys,
My fileupload function works pretty fine in the GWT default testing
environment jetty, but when I compile the project to js files, and
deploy it to TOMCAT, it does not work.
When I click the Submit button, only the Window.alert() runs, but no
file been uploaded.
Below is all the related files:
GWT client code:
--------------------------------------------------------------------------------
package cssp.web.client;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
public class FileDialog extends DialogBox{
private int taskID = -1;
public FileDialog(int id) {
super();
this.taskID = id;
setAnimationEnabled(true);
setText("Upload File");
final FormPanel fileForm = new FormPanel();
fileForm.setAction("/csspweb/fileUpload");
fileForm.setEncoding(FormPanel.ENCODING_MULTIPART);
fileForm.setMethod(FormPanel.METHOD_POST);
VerticalPanel holder = new VerticalPanel();
HorizontalPanel buttons = new HorizontalPanel();
Hidden hide = new Hidden();
hide.setName("HideTaskID");
hide.setValue(taskID + "");
fileForm.setWidget(holder);
final FileUpload upload = new FileUpload();
upload.setName("uploadfile");
holder.add(upload);
holder.add(hide);
buttons.setSpacing(5);
Button submit = new Button();
submit.setText("Submit");
submit.setWidth("100px");
submit.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent arg0) {
Window.alert("hhhhhhhhhhhhhhhhhhh");
fileForm.submit();
}
});
// buttons.add(new Button("Submit", new ClickHandler() {
// public void onClick(ClickEvent arg0) {
// // TODO Auto-generated method stub
// Window.alert("here submit");
// fileForm.submit();
// }
// }) );
Button close = new Button();
close.setText("Close");
close.setWidth("100px");
close.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent arg0) {
hide();
}
});
buttons.add(submit);
buttons.add(close);
// buttons.add(new Button("Close", new ClickHandler() {
// public void onClick(ClickEvent arg0) {
// hide();
// }
// }));
holder.add(buttons);
fileForm.addSubmitHandler(new FormPanel.SubmitHandler() {
public void onSubmit(SubmitEvent event) {
// TODO Auto-generated method stub
if (upload.getFilename().length() == 0) {
Window.alert("Please secify the file to
upload!");
event.cancel();
}
Window.alert("submited");
}
});
fileForm.addSubmitCompleteHandler(new
FormPanel.SubmitCompleteHandler
() {
public void onSubmitComplete(SubmitCompleteEvent arg0) {
if (arg0.getResults() == null ||
arg0.getResults().equals("")) {
UserPanel userPanel = CsspWeb.userPanel;
Window.alert("File Uploaded
Successfully!");
userPanel.setContent(new TasksPanel());
hide();
}
}
});
setWidget(fileForm);
}
public void showIt() {
center();
show();
}
}
web.xml
-------------------------------------------------------
<servlet-mapping>
<servlet-name>fileUploadServlet</servlet-name>
<url-pattern>/csspweb/fileUpload</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>fileUploadServlet</servlet-name>
<servlet-class>cssp.web.server.FileUploadImpl</servlet-class>
</servlet>
server side code:
--------------------------------------------------------
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse
resp)
throws ServletException, IOException {
String id = req.getParameter("HideTaskID");
String sid = (String)req.getAttribute("HideTaskID");
System.out.println("========================= " + id + "::" +
sid);
File newFile = null;
if (ServletFileUpload.isMultipartContent(req)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096);
System.out.println("=====================");
factory.setRepository(new File(""));
ServletFileUpload upload = new
ServletFileUpload(factory);
upload.setSizeMax(100000000);
System.out.println(factory.getRepository().getAbsolutePath());
try {
List items = upload.parseRequest(req);
Iterator iter = items.iterator();
while(iter.hasNext()) {
System.out.println("******************************** ");
FileItem file = (FileItem)iter.next();
InputStream inStream =
file.getInputStream();
if (!file.isFormField()) {
String allName = file.getName();
//处理文件名
String fileName =
allName.substring(allName.lastIndexOf("\\"),
allName.length());
newFile = new
File(SysProperties.readValue("temp.repo.upload"),
fileName);
//读取文件内容
BufferedInputStream bis = new
BufferedInputStream(inStream);
BufferedOutputStream bos = new
BufferedOutputStream(new
FileOutputStream(newFile));
byte[] bytes = file.get();
while(bis.read(bytes) != -1) {
bos.write(bytes);
}
bos.flush();
bis.close();
bos.close();
inStream.close();
System.out.println(newFile.getAbsolutePath());
} else {
if
(file.getFieldName().equals("HideTaskID")) {
taskID =
Integer.parseInt(file.getString());
// System.out.println("It
is ittttttttttttttttttt" + taskID);
}
}
}
//将该文件上传至ftp
// String filePath = transferToFtp(newFile);
//
// //将上传文件的路径添加到Task信息中
// addFilePath(taskID, filePath);
//
// //修改数据库中任务状态
// changeTaskStatus(taskID,
ServerConstants.TASK_COMPLETED_S);
return;
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Any help will be appreciated
Thank you
xiaoyang
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---