GWT实现文件上传下载
关键字: gwt实现文件上传 下载
主要采用的两个包gwtextux.jar(0.4版本)和commons- fileupload-1.2.1.jar来完成。
在gwt.xml里添加此内容
如你所见,相关JS(在gwtextux.jar包里)放在public/js/下面,与 xml对应
Xml代码
1. <module>
2. <entry-point class='com.google.gwt.client.test'/>
3. <stylesheet src='test.css' />
4. <stylesheet src="js/resources/css/ext-all.css" />
5. <script src="js/adapter/ext/ext-base.js" />
6. <script src="js/ext-all.js" />
7. <inherits name="com.gwtext.GwtExt"/>
8.
9.
10.
11. <!-- upload -->
12. <servlet path="/UploadSvc" class="com.google.gwt.service.UploadSvcImpl" />
13. <inherits name="com.gwtextux.GwtExtUx"/>
14. <inherits name="com.gwtextux.UploadDialog"/>
15. <script src="js/Ext.ux.UploadDialog.js" />
16. <!-- upload -->
17.
18. </module>
<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>
客户端:
Java代码
1. import com.google.gwt.core.client.EntryPoint;
2. import com.google.gwt.user.client.ui.RootPanel;
3. import com.gwtext.client.widgets.Button;
4. import com.gwtext.client.widgets.Panel;
5. import com.gwtext.client.widgets.event.ButtonListenerAdapter;
6. import com.gwtext.client.core.EventObject;
7. import com.gwtext.client.core.UrlParam;
8. import com.gwtextux.client.widgets.upload.UploadDialog;
9.
10. public class UploadDialogSample implements EntryPoint{
11.
12. public void onModuleLoad() {
13. Panel panel = new Panel();
14. Button button = new Button("Upload File", new ButtonListenerAdapter() {
15. public void onClick(final Button button, EventObject e) {
16. UploadDialog dialog = new UploadDialog();
17. //于xml对应
18. dialog.setUrl("UploadSvc");
19. //上传文件类型
20. //dialog.setPermittedExtensions(new String[]{"jpg", "gif"});
21. //参数
22. UrlParam param[] = new UrlParam[2];
23. param[0]=new UrlParam("name1", "value1");
24. param[1]=new UrlParam("name2", "value2");
25. dialog.setBaseParams(param);
26. //上传数据名称 并非文件名
27. dialog.setPostVarName("myvar");
28. dialog.show();
29. }
30. });
31. panel.add(button);
32. RootPanel.get().add(panel);
33.
34. }
35. }
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);
}
}
服务端:
Java代码
1. import java.io.BufferedInputStream;
2. import java.io.BufferedOutputStream;
3. import java.io.File;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6. import java.io.InputStream;
7. import javax.servlet.ServletException;
8. import javax.servlet.http.HttpServlet;
9. import javax.servlet.http.HttpServletRequest;
10. import javax.servlet.http.HttpServletResponse;
11.
12. import org.apache.commons.fileupload.FileItemIterator;
13. import org.apache.commons.fileupload.FileItemStream;
14. import org.apache.commons.fileupload.servlet.ServletFileUpload;
15. import org.apache.commons.fileupload.util.Streams;
16.
17. import com.google.gwt.core.client.GWT;
18.
19. public class UploadSvcImpl extends HttpServlet {
20.
21. private static final long serialVersionUID = 1L;
22.
23. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
24. throws ServletException, IOException {
25.
26. /*
27. * The following commented code uses Apache Commons Upload
28. */
29. // Check that we have a file upload request
30. boolean isMultipart = ServletFileUpload.isMultipartContent(req);
31. GWT.log("is multipart? " + Boolean.toString(isMultipart), null);
32. // Create a new file upload handler
33. ServletFileUpload upload = new ServletFileUpload();
34.
35. try {
36. // Parse the request
37. FileItemIterator iter = upload.getItemIterator(req);
38. while (iter.hasNext()) {
39. FileItemStream item = iter.next();
40. String name = item.getFieldName();
41. InputStream stream = item.openStream();
42. if (item.isFormField()) {
43. System.out.println("Form field " + name + " with value "
44. + Streams.asString(stream) + " detected.");
45. } else {
46. System.out.println("File field " + name
47. + " with file name " + item.getName()
48. + " detected.");
49. BufferedInputStream inputStream = new BufferedInputStream(
50. stream);// 获得输入流
51. BufferedOutputStream outputStream = new BufferedOutputStream(
52. new FileOutputStream(new File("/var/app/fax/"
53. + item.getName())));// 获得文件输出流
54. Streams.copy(inputStream, outputStream, true); // 开始把文件写到你指定的上传文件夹
55. }
56. stream.close();
57. }
58. System.out.println("上传成功!");
59. } catch (Exception ex) {
60. ex.printStackTrace();
61. }
62. }
63.
64. }
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里双击触发下载
Java代码
1. protected void onGridRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
2. // test.rar在 public目录下
3. Window.open(GWT.getHostPageBaseURL()+"test.rar", "", "");
4. }
protected void onGridRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
// test.rar在 public目录下
Window.open(GWT.getHostPageBaseURL()+"test.rar", "", "");
}
* commons-fileupload-1.2.1.jar (56.4 KB)
* 下载次数: 113
* gwtextux.jar (1.4 MB)
* 下载次数: 219
[size=large][/size][size=x-large][/size][size=large][/size]
关键字: gwt实现文件上传 下载
主要采用的两个包gwtextux.jar(0.4版本)和commons- fileupload-1.2.1.jar来完成。
在gwt.xml里添加此内容
如你所见,相关JS(在gwtextux.jar包里)放在public/js/下面,与 xml对应
Xml代码
1. <module>
2. <entry-point class='com.google.gwt.client.test'/>
3. <stylesheet src='test.css' />
4. <stylesheet src="js/resources/css/ext-all.css" />
5. <script src="js/adapter/ext/ext-base.js" />
6. <script src="js/ext-all.js" />
7. <inherits name="com.gwtext.GwtExt"/>
8.
9.
10.
11. <!-- upload -->
12. <servlet path="/UploadSvc" class="com.google.gwt.service.UploadSvcImpl" />
13. <inherits name="com.gwtextux.GwtExtUx"/>
14. <inherits name="com.gwtextux.UploadDialog"/>
15. <script src="js/Ext.ux.UploadDialog.js" />
16. <!-- upload -->
17.
18. </module>
<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>
客户端:
Java代码
1. import com.google.gwt.core.client.EntryPoint;
2. import com.google.gwt.user.client.ui.RootPanel;
3. import com.gwtext.client.widgets.Button;
4. import com.gwtext.client.widgets.Panel;
5. import com.gwtext.client.widgets.event.ButtonListenerAdapter;
6. import com.gwtext.client.core.EventObject;
7. import com.gwtext.client.core.UrlParam;
8. import com.gwtextux.client.widgets.upload.UploadDialog;
9.
10. public class UploadDialogSample implements EntryPoint{
11.
12. public void onModuleLoad() {
13. Panel panel = new Panel();
14. Button button = new Button("Upload File", new ButtonListenerAdapter() {
15. public void onClick(final Button button, EventObject e) {
16. UploadDialog dialog = new UploadDialog();
17. //于xml对应
18. dialog.setUrl("UploadSvc");
19. //上传文件类型
20. //dialog.setPermittedExtensions(new String[]{"jpg", "gif"});
21. //参数
22. UrlParam param[] = new UrlParam[2];
23. param[0]=new UrlParam("name1", "value1");
24. param[1]=new UrlParam("name2", "value2");
25. dialog.setBaseParams(param);
26. //上传数据名称 并非文件名
27. dialog.setPostVarName("myvar");
28. dialog.show();
29. }
30. });
31. panel.add(button);
32. RootPanel.get().add(panel);
33.
34. }
35. }
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);
}
}
服务端:
Java代码
1. import java.io.BufferedInputStream;
2. import java.io.BufferedOutputStream;
3. import java.io.File;
4. import java.io.FileOutputStream;
5. import java.io.IOException;
6. import java.io.InputStream;
7. import javax.servlet.ServletException;
8. import javax.servlet.http.HttpServlet;
9. import javax.servlet.http.HttpServletRequest;
10. import javax.servlet.http.HttpServletResponse;
11.
12. import org.apache.commons.fileupload.FileItemIterator;
13. import org.apache.commons.fileupload.FileItemStream;
14. import org.apache.commons.fileupload.servlet.ServletFileUpload;
15. import org.apache.commons.fileupload.util.Streams;
16.
17. import com.google.gwt.core.client.GWT;
18.
19. public class UploadSvcImpl extends HttpServlet {
20.
21. private static final long serialVersionUID = 1L;
22.
23. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
24. throws ServletException, IOException {
25.
26. /*
27. * The following commented code uses Apache Commons Upload
28. */
29. // Check that we have a file upload request
30. boolean isMultipart = ServletFileUpload.isMultipartContent(req);
31. GWT.log("is multipart? " + Boolean.toString(isMultipart), null);
32. // Create a new file upload handler
33. ServletFileUpload upload = new ServletFileUpload();
34.
35. try {
36. // Parse the request
37. FileItemIterator iter = upload.getItemIterator(req);
38. while (iter.hasNext()) {
39. FileItemStream item = iter.next();
40. String name = item.getFieldName();
41. InputStream stream = item.openStream();
42. if (item.isFormField()) {
43. System.out.println("Form field " + name + " with value "
44. + Streams.asString(stream) + " detected.");
45. } else {
46. System.out.println("File field " + name
47. + " with file name " + item.getName()
48. + " detected.");
49. BufferedInputStream inputStream = new BufferedInputStream(
50. stream);// 获得输入流
51. BufferedOutputStream outputStream = new BufferedOutputStream(
52. new FileOutputStream(new File("/var/app/fax/"
53. + item.getName())));// 获得文件输出流
54. Streams.copy(inputStream, outputStream, true); // 开始把文件写到你指定的上传文件夹
55. }
56. stream.close();
57. }
58. System.out.println("上传成功!");
59. } catch (Exception ex) {
60. ex.printStackTrace();
61. }
62. }
63.
64. }
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里双击触发下载
Java代码
1. protected void onGridRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
2. // test.rar在 public目录下
3. Window.open(GWT.getHostPageBaseURL()+"test.rar", "", "");
4. }
protected void onGridRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
// test.rar在 public目录下
Window.open(GWT.getHostPageBaseURL()+"test.rar", "", "");
}
* commons-fileupload-1.2.1.jar (56.4 KB)
* 下载次数: 113
* gwtextux.jar (1.4 MB)
* 下载次数: 219
[size=large][/size][size=x-large][/size][size=large][/size]