主要采用的两个包gwtextux.jar(0.4版本)和commons-fileupload-1.2.1.jar来完成。
在gwt.xml里添加此内容
如你所见,相关JS(在gwtextux.jar包里)放在public/js/下面,与xml对应
<module> <entry-point class='com.google.gwt.client.test'/> <stylesheet src='test.css' /> <stylesheet src="js/resources/css/ext-all.css" /> <script src="js/adapter/ext/ext-base.js" /> <script src="js/ext-all.js" /> <inherits name="com.gwtext.GwtExt"/> <!-- upload --> <servlet path="/UploadSvc" class="com.google.gwt.service.UploadSvcImpl" /> <inherits name="com.gwtextux.GwtExtUx"/> <inherits name="com.gwtextux.UploadDialog"/> <script src="js/Ext.ux.UploadDialog.js" /> <!-- upload --> </module>
客户端:
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.user.client.ui.RootPanel; import com.gwtext.client.widgets.Button; import com.gwtext.client.widgets.Panel; import com.gwtext.client.widgets.event.ButtonListenerAdapter; import com.gwtext.client.core.EventObject; import com.gwtext.client.core.UrlParam; import com.gwtextux.client.widgets.upload.UploadDialog; public class UploadDialogSample implements EntryPoint{ public void onModuleLoad() { Panel panel = new Panel(); Button button = new Button("Upload File", new ButtonListenerAdapter() { public void onClick(final Button button, EventObject e) { UploadDialog dialog = new UploadDialog(); //于xml对应 dialog.setUrl("UploadSvc"); //上传文件类型 //dialog.setPermittedExtensions(new String[]{"jpg", "gif"}); //参数 UrlParam param[] = new UrlParam[2]; param[0]=new UrlParam("name1", "value1"); param[1]=new UrlParam("name2", "value2"); dialog.setBaseParams(param); //上传数据名称 并非文件名 dialog.setPostVarName("myvar"); dialog.show(); } }); panel.add(button); RootPanel.get().add(panel); } }
服务端:
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; import com.google.gwt.core.client.GWT; public class UploadSvcImpl extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* * The following commented code uses Apache Commons Upload */ // Check that we have a file upload request boolean isMultipart = ServletFileUpload.isMultipartContent(req); GWT.log("is multipart? " + Boolean.toString(isMultipart), null); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); try { // Parse the request FileItemIterator iter = upload.getItemIterator(req); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); InputStream stream = item.openStream(); if (item.isFormField()) { System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected."); } else { System.out.println("File field " + name + " with file name " + item.getName() + " detected."); BufferedInputStream inputStream = new BufferedInputStream( stream);// 获得输入流 BufferedOutputStream outputStream = new BufferedOutputStream( new FileOutputStream(new File("/var/app/fax/" + item.getName())));// 获得文件输出流 Streams.copy(inputStream, outputStream, true); // 开始把文件写到你指定的上传文件夹 } stream.close(); } System.out.println("上传成功!"); } catch (Exception ex) { ex.printStackTrace(); } } }
你也可以直接访问http://max-bazhenov.com/dev/upload-dialog-2.0/index.php
下载:
grid里双击触发下载
protected void onGridRowDblClick(GridPanel grid, int rowIndex, EventObject e) { // test.rar在 public目录下 Window.open(GWT.getHostPageBaseURL()+"test.rar", "", ""); }