Browse Source

add for upload

Lu 9 years ago
parent
commit
982e4699c0

+ 1 - 0
display-server/etc/server-page.xml

@ -21,6 +21,7 @@
21 21
	<action name="UI-CustomDialog" template="template/webapp/plugin/UI-CustomDialog.html"></action>
22 22
	<action name="UI-CustomWindow" template="template/webapp/plugin/UI-CustomWindow.html"></action>
23 23
	<action name="UI-SlidingMenu" template="template/webapp/plugin/UI-SlidingMenu.html"></action>
24
	<action name="FileUpload" template="template/webapp/plugin/FileUpload.html"></action>
24 25
	<!-- Web Component Demo-->
25 26
	<action name="WmTab" template="template/webapp/tag/WmTab.html"></action>
26 27
	<action name="WmTabbar" template="template/webapp/tag/WmTabbar.html"></action>

+ 101 - 4
display-server/src/com/ai/server/bean/TestBean.java

@ -1,8 +1,22 @@
1 1
package com.ai.server.bean;
2 2

3
import java.io.File;
4
import java.io.FileOutputStream;
5
import java.io.InputStream;
6
import java.io.OutputStream;
7
import java.util.List;
8

9
import javax.servlet.http.HttpServletRequest;
10

11
import org.apache.commons.fileupload.FileItem;
12
import org.apache.commons.fileupload.FileUploadException;
13
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
14
import org.apache.commons.fileupload.servlet.ServletFileUpload;
15

3 16
import com.ai.server.core.bean.DisplayBean;
4 17
import com.ailk.common.data.IData;
5 18
import com.ailk.common.data.impl.DataMap;
19
import com.ailk.mobile.servlet.ServletManager;
6 20

7 21
/**
8 22
 * a test bean
@ -11,11 +25,94 @@ import com.ailk.common.data.impl.DataMap;
11 25
 *
12 26
 */
13 27
public class TestBean extends DisplayBean {
14
	public IData receive ( IData param ) {
15
		System.out.println( "======= TestBean.receive() " + param );
16
		return param;
17
	}
18 28
	
29
	public IData receive(IData param) {
30
		HttpServletRequest request = ServletManager.getRequest();
31
		//获得磁盘文件条目工厂
32
		DiskFileItemFactory factory = new DiskFileItemFactory();
33
		//获取文件需要上传到的路径
34
		String path = request.getRealPath("/upload");
35
       
36
       //如果没以下两行设置的话,上传大的 文件 会占用 很多内存  
37
       //设置暂时存放的 存储室 , 这个存储室,可以和 最终存储文件的目录不同
38
       /**
39
        * 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上
40
        * 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem 格式的
41
        * 然后再将其真正写到 对应目录的硬盘上
42
        */
43
       factory.setRepository(new File(path));
44
       //设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到暂时存储室  
45
       factory.setSizeThreshold(1024*1024);
46
       //文件上传处理
47
       ServletFileUpload upload = new ServletFileUpload(factory);
48

49
	   List<FileItem> fileList = null;
50
       try {
51
	       boolean isMultipart = upload.isMultipartContent(request); 
52
	       if (isMultipart) {
53
	    	   fileList = upload.parseRequest(request);  
54
	       }else{
55
	    	   System.out.println(" { 'result': false, 'error': 网络异常,请重新上传!");
56
	    	   System.out.println(" }");
57
	    	   return null;
58
	       }
59
	       
60
           for(FileItem item : fileList){
61
               //获取表单的属性名字
62
               String name = item.getFieldName();
63
               
64
               //如果获取的 表单信息是普通的文本信息
65
               if(item.isFormField()){
66
                   //获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的
67
                   String value = item.getString() ;
68
                   request.setAttribute(name, value);
69
                   
70
               //对传入的非简单的字符串进行处理 ,比如说二进制的 图片,电影这些
71
               }else{
72
                   /**
73
                    * 以下三步,主要获取 上传文件的名字
74
                    */
75
                   //获取路径名
76
                   String value = item.getName();  
77
                   //索引到最后一个反斜杠
78
                   int start = value.lastIndexOf("\\"); 
79
                   //截取 上传文件的 字符串名字,加1是 去掉反斜杠
80
                   String filename = value.substring(start + 1);
81
                   request.setAttribute(name, filename);
82
                   
83
                   //真正写到磁盘上
84
                   //它抛出的异常 用exception 捕捉
85
                   //item.write( new File(path,filename) );//第三方提供的
86
                   //手动写的
87
                   OutputStream out = new FileOutputStream(new File(path,filename));  
88
                   
89
                   InputStream in = item.getInputStream();
90
                   
91
                   int length = 0;
92
                   byte [] buf = new byte[1024];
93
                   
94
                   System.out.println("获取上传文件的总共的容量:"+item.getSize());
95
                   
96
                   // in.read(buf) 每次读到的数据存放在   buf 数组中
97
                   while( (length = in.read(buf) ) != -1) {
98
                       //在 buf 数组中 取出数据 写到 (输出流)磁盘上
99
                       out.write(buf, 0, length);
100
                   }
101
                   in.close(); 
102
                   out.close();
103
               }
104
           }
105
           
106
       } catch (FileUploadException e) {
107
           // TODO Auto-generated catch block
108
           e.printStackTrace();
109
       }catch (Exception e) {
110
           // TODO Auto-generated catch block  
111
           e.printStackTrace();
112
       }
113
       
114
       return null;
115
	}
19 116
	/****
20 117
	 * 将参数反转
21 118
	 * @param param

+ 3 - 0
display-server/web/res/js/mobile/expand-mobile.js

@ -216,6 +216,9 @@ define(["require"],function(require) {
216 216
			},aliPay:function(tradeNo,subject,body,price,callback,err){
217 217
				storageCallback("aliPay",callback);
218 218
				execute("aliPay",[tradeNo,subject,body,price],err);	
219
			},uploadFile:function(filePath,callback,err){
220
				storageCallback("uploadFile",callback);
221
				execute("uploadFile",[filePath],err);	
219 222
			}
220 223
		};
221 224
	})();

+ 4 - 0
display-server/web/template/webapp/PluginIndex.html

@ -39,4 +39,8 @@
39 39
		<div class="pic"><span class="e_ico-conect"></span></div>
40 40
		<div class="text">数据库</div>
41 41
	</li>
42
	<li action="FileUpload">
43
		<div class="pic"><span class="e_ico-storage"></span></div>
44
		<div class="text">文件上传</div>
45
	</li>
42 46
</ul>