首先下载两个包 commons-fileupload-?.jar和commons-io-?.jar 将他们配置到你的项目中
上传代码:
client 端代码:
final FileUpload fileUpload = new FileUpload(); final FormPanel formpanel = new FormPanel(); fileUpload.setName("uploadFormElement"); formpanel.setEncoding( FormPanel.ENCODING_MULTIPART ); formpanel.setMethod( FormPanel.METHOD_POST ); formpanel.setAction( GWT.getModuleBaseURL() + "fileupload" ); formpanel.setWidget( fileUpload ); uploadbutton.addClickHandler( new ClickHandler() { public void onClick( ClickEvent sender ) { formpanel.submit(); } }); formpanel.addSubmitHandler( new SubmitHandler() { public void onSubmit(SubmitEvent event) { if( fileUpload.getFilename().length() == 0 ) { Window.alert( "you must select a file" ); } } }); formpanel.addSubmitCompleteHandler( new SubmitCompleteHandler() { public void onSubmitComplete(SubmitCompleteEvent event) { //兼容linux文件分隔符? String selected = fileUpload.getFilename().replaceAll("/", "//"); selected = selected.substring( selected.lastIndexOf("//")+1); Window.alert( "upload complete"+ event.getResults()); } });之后再server中添加服务 :FileUploadServlet.java
package sample.server; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = null; try { items = upload.parseRequest(request); } catch (FileUploadException ex) { ex.printStackTrace(); } Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); String savePath = getServletContext().getRealPath("/uploads"); String filename = item.getName(); String filename2 = filename.replaceAll("/", "//"); int pos = filename.lastIndexOf( "//") + 1; File file = new File(savePath,filename.substring( pos ) ); try { item.write(file); } catch (Exception e) { e.printStackTrace(); } } } }打开web.xml 加入
<servlet> <servlet-name>uploadServlet</servlet-name> <servlet-class>xxx.xxx.FileUploadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>uploadServlet</servlet-name> <url-pattern>fileupload</url-pattern> </servlet-mapping>
两个servlet-name 相同即可 而servlet-class对应之前添加的服务,url-pattern对应 client端中的
formpanel.setAction( GWT.getModuleBaseURL() + "fileupload" );
打开 xxx.gwt.xml 加入 :<servlet class="sample.server.FileUploadServlet" path="/fileupload" />
class 和 path也是对应的,别搞错。
下载代码;
client中代码:
void download( String filename ) { if( RootPanel.get("downloadiframe") != null ) { Widget widgetFrame = (Widget)RootPanel.get("downloadiframe"); widgetFrame.removeFromParent(); } Frame frame = new Frame( GWT.getModuleBaseURL() + "filedownload" + "?id=downloadiframe"+ "&filename=" + filename ); frame.setVisible( false ); frame.setSize( "0px", "0px" ); RootPanel.get().add( frame ); }服务器server:DownloadServlet.java
package sample.server; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.ui.Frame; public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { //此函数对应之前的 Frame frame = //new Frame( GWT.getModuleBaseURL() + "filedownload" + "?id=downloadiframe"+"&filename=" + filename ); //传文件名那个简单啊 String filename = req.getParameter("filename"); //这边你要自己决定从哪下载的文件!! 我是把上传路径直接给他了 //这里说下。。我先前没自己创建 uploads 结果十分悲剧 //记得在 war 路径下创建 !! String filepath = req.getRealPath("/uploads"); File file = new File( filepath+"/"+filename ); //下面代码从网上拉的,直接抄就行了 FileInputStream fis = new FileInputStream(file); resp.addHeader("Content-Disposition","attachment; filename=" + filename ); ServletOutputStream out = resp.getOutputStream(); resp.setBufferSize(32768); int bufSize = resp.getBufferSize(); byte[] buffer = new byte[bufSize]; BufferedInputStream bis = new BufferedInputStream(fis,bufSize); int bytes; while ((bytes = bis.read(buffer, 0, bufSize)) >= 0) out.write(buffer, 0, bytes); bis.close(); fis.close(); out.flush(); out.close(); } }配置两个xml和upload是一样的,在此不赘述。