huangbo 9 lat temu
rodzic
commit
365f1e28f5

+ 1 - 1
display-client/assets/mobile-config.xml

@ -15,7 +15,7 @@
15 15
	<!-- 非必须, 默认false。是否开启多WebView模式 -->
16 16
	<config name="is_mult_webview" value="true"/>
17 17
	<!-- 必须。应用license -->
18
	<config name="license" value="DshTMgSyj5B9ww32G72OhQ2yUv4Qp3PHHpqG8WX9LugEMQb2/CBRLUXzBHX9ro1WWNDqT9UICi8B24eYyykNosV4yk1SgYyDL61leCi-UzHY3dbcG1hJqZlZpvL2kQUqQVBo-o83KY8VlPGgY-Ci1gHJuomqI-I03vlkhv0bvec=|@@MDA5QkQ3MUQxNkQ0NTE3QzhGNEVGNDk4OTk2MzU0QkM0NEFERjc0ODQwOEU5NDFFOTQ2RUJFNDdFNTAyMTI1QTYzNDdFQTJFMjAxRURGRjFDQzcxNjRGNzlERTFFNDBBMTY3OEM5QkE1NDhBOUM3NzNBMDI1RUY2N0QyMTlFOUNDRjdEMDAyRDI2QzVDQTQ4NTREMDlFMDMwNjJCOTM5OTRFQTJBOUU3RkYxMEYwMzE5NUYxNjU0RTUzMTVFMUM1QjYxMzY2REYwOUFGNDI5QkMzRTY2MzA5QzNEMTVEMEUxNUM2MUNGQTk4NTk3RDAzRjY2OEEzMjNFNDc0ODA3MTY5" ></config>
18
	<config name="license" value="DrvY4GdwIo9AaV91hnnxlOv4N8NbuH7SQmf6St8uUi7q5VH00fbTkCsPkOkMy6u2HJ75Vk8gFNtzKuJbav3Pb3g0idpr8ORWWS7-tZFAlZb4nAvQ4LNBAwnYCD168PpgGc-DQIutWO8r9gxWpASkdrHn6lqOdu9V4R-/SWpqcF8=|@@MDA5NThBRjc4QTEzMjc1MDYwQjFGNzc3Qzk3OEI5RUQzNENENUU1RkVDNzk4NjhBQ0IzMDczMjYzRDM3Mzc0Qjk2NkY0RkQ5NjBCQTcwNjNFQ0YzRkJCMzAyNjczNDJCQjI1RDg1RkUwQTA4MjA4QkFCRDIzQ0ZCQkYzMjJBRjhDOUQzRTUzMEQ2OENBM0UxREZDOTMxOTEzQzFGNDY1QzZCRTUxNDFCMTQwRDU4M0E1ODc0RTYyMzI0ODdCMUY3ODNBRDJGNDRERkNFQjBCQjY0OTkxQkYzRDczNUI2RTc0OUE2QjNFQkZCNUJDMTM4M0MxRUEyRjZCOTg1Q0FBMDE3" ></config>
19 19
	<!-- 非必须。进入应用时的欢迎页面 --><!-- file:///android_asset为固定写法,表示工程的asset目录 -->
20 20
	<config name="loading_page" value="file:///android_asset/local/welcome.html"/>
21 21
	<!-- 非必须。更新应用的url -->

+ 2 - 0
display-server/etc/server-data.xml

@ -6,4 +6,6 @@
6 6
	
7 7
	<action name="receiveData" class="com.ai.server.bean.TestBean" method="receive" verify="false"></action>
8 8
	<action name="TestBean.reverse" class="com.ai.server.bean.TestBean" method="reverse" verify="false"></action>
9
	
10
	<action name="FileUpDownload.upload" class="com.ai.server.bean.FileUpDownload" method="upload" verify="false"></action>
9 11
</datas>

+ 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>

+ 187 - 0
display-server/src/com/ai/server/bean/FileUpDownload.java

@ -0,0 +1,187 @@
1
package com.ai.server.bean;
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

16
import com.ai.server.core.bean.DisplayBean;
17
import com.ai.server.util.ApplicationPath;
18
import com.ailk.common.data.IData;
19
import com.ailk.mobile.servlet.ServletManager;
20

21
/**
22
 * 
23
 * @author Lu
24
 *
25
 */
26
public class FileUpDownload extends DisplayBean {
27
	/**
28
	 * 文件上传
29
	 * @param param
30
	 * @return
31
	 * @throws Exception
32
	 */
33
	public IData upload(IData param) throws Exception{
34
		HttpServletRequest request = ServletManager.getRequest();
35
		//获取文件需要上传到的路径
36
		String userDir = "default";//此处一般使用用户名管理文件
37
		String path = ApplicationPath.getFilePath(request) + "upload" + File.separator + userDir;
38
		
39
		File file = new File(path);
40
		if(! file.exists() && ! file.mkdirs()){
41
			throw new RuntimeException("生成用户上传文件夹失败,无法进行上传操作!");
42
		}
43
		
44
		//获得磁盘文件条目工厂
45
		DiskFileItemFactory factory = new DiskFileItemFactory();
46
        //如果没以下两行设置的话,上传大的文件会占用很多内存
47
        //设置暂时存放的存储室,这个存储室可以和最终存储文件的目录不同
48
        /**
49
         * 原理它是先存到暂时存储室,然后在真正写到对应目录的硬盘上
50
         * 按理来说当上传一个文件时,其实是上传了两份,第一个是以.tem格式的
51
         * 然后再将其真正写到对应目录的硬盘上
52
         */
53
         factory.setRepository(file);
54
         //设置缓存的大小,当上传文件的容量超过该缓存时,直接放到暂时存储室
55
         factory.setSizeThreshold(1024*1024);
56
         //文件上传处理
57
         ServletFileUpload upload = new ServletFileUpload(factory);
58
         
59
         try {
60
	         if (! ServletFileUpload.isMultipartContent(request)) {
61
	    	 	 log.debug("{'result': false, 'error': 网络异常,请重新上传!}");
62
	    	     return null;
63
	         }
64
	         List<?> fileList = upload.parseRequest(request);
65
	         
66
             for(int i = 0; i < fileList.size(); i++){
67
             	 FileItem item = (FileItem)fileList.get(i);
68
                 //获取表单的属性名字
69
                 String name = item.getFieldName();
70
                 
71
            	 //如果获取的 表单信息是普通的文本信息
72
                 if(item.isFormField()){
73
                     //获取用户具体输入的字符串,因为表单提交过来的是字符串类型的
74
                     String value = item.getString();
75
                     request.setAttribute(name, value);
76
                 	 
77
                 }else{
78
	                 //获取路径名
79
	                 String value = item.getName();
80
	                 //索引到最后一个反斜杠
81
	                 int start = value.lastIndexOf("\\");
82
	                 //截取 上传文件的 字符串名字,加1是 去掉反斜杠
83
	                 String filename = value.substring(start + 1);
84
	                 request.setAttribute(name, filename);
85
	                 
86
	                 //写到磁盘上
87
	                 OutputStream out = new FileOutputStream(new File(path,filename));
88
	                 InputStream in = item.getInputStream();
89
	                 
90
	                 int length = 0;
91
	                 byte[] buf = new byte[1024];
92
	                 
93
	                 System.out.println("获取上传文件的总共的容量:" + item.getSize());
94
	                 //写出到文件中
95
	                 while((length = in.read(buf) ) != -1) {
96
	                     //在BUF数组中取出数据写到(输出流)磁盘上
97
	                     out.write(buf, 0, length);
98
	                 }
99
	                 in.close();
100
	                 out.close();
101
                }
102
            }
103
        } catch (FileUploadException e) {
104
            e.printStackTrace();
105
            
106
        }catch (Exception e) {
107
            e.printStackTrace();
108
        }
109
        return null;
110
	}
111
	/**
112
	 * 文件下载
113
	 * @param param
114
	 * @return
115
	 * @throws Exception
116
	 */
117
	public IData download(IData param) throws Exception{
118
		HttpServletRequest request = ServletManager.getRequest();
119
		//获取文件需要上传到的路径
120
		String appPath = ApplicationPath.getFilePath(request);
121
		String path = appPath + File.separator + "upload" + File.separator + "Steven";
122
		File file = new File(path);
123
		if(! file.exists() && ! file.mkdirs()){
124
			throw new RuntimeException("生成用户上传文件夹失败,无法进行上传操作!");
125
		}
126
		
127
		//获得磁盘文件条目工厂
128
		DiskFileItemFactory factory = new DiskFileItemFactory();
129
       //如果没以下两行设置的话,上传大的文件会占用很多内存
130
       //设置暂时存放的存储室,这个存储室可以和最终存储文件的目录不同
131
       /**
132
        * 原理它是先存到暂时存储室,然后在真正写到对应目录的硬盘上
133
        * 按理来说当上传一个文件时,其实是上传了两份,第一个是以.tem格式的
134
        * 然后再将其真正写到对应目录的硬盘上
135
        */
136
        factory.setRepository(file);
137
        //设置缓存的大小,当上传文件的容量超过该缓存时,直接放到暂时存储室
138
        factory.setSizeThreshold(1024*1024);
139
        //文件上传处理
140
        ServletFileUpload upload = new ServletFileUpload(factory);
141
        
142
        try {
143
	        if (! ServletFileUpload.isMultipartContent(request)) {
144
	    		System.out.println("{'result': false, 'error': 网络异常,请重新上传!}");
145
	    	    return null;
146
	        }
147
	        List<?> fileList = upload.parseRequest(request);
148
	        
149
            for(int i = 0; i < fileList.size(); i++){
150
            	
151
            	FileItem item = (FileItem)fileList.get(i);
152
                //获取表单的属性名字
153
                String name = item.getFieldName();
154
                /**以下三步,主要获取上传文件的名字*/
155
                //获取路径名
156
                String value = item.getName();
157
                //索引到最后一个反斜杠
158
                int start = value.lastIndexOf("\\");
159
                //截取 上传文件的 字符串名字,加1是 去掉反斜杠
160
                String filename = value.substring(start + 1);
161
                request.setAttribute(name, filename);
162
                
163
                //写到磁盘上
164
                OutputStream out = new FileOutputStream(new File(path,filename));
165
                InputStream in = item.getInputStream();
166
                
167
                int length = 0;
168
                byte[] buf = new byte[1024];
169
                
170
                System.out.println("获取上传文件的总共的容量:" + item.getSize());
171
                //in.read(BUF)每次读到的数据存放在BUF数组中
172
                while((length = in.read(buf) ) != -1) {
173
                    //在BUF数组中取出数据写到(输出流)磁盘上
174
                    out.write(buf, 0, length);
175
                }
176
                in.close();
177
                out.close();
178
            }
179
        } catch (FileUploadException e) {
180
            e.printStackTrace();
181
            
182
        }catch (Exception e) {
183
            e.printStackTrace();
184
        }
185
        return null;
186
	}
187
}

+ 56 - 0
display-server/src/com/ai/server/util/ApplicationPath.java

@ -0,0 +1,56 @@
1
package com.ai.server.util;
2
3
import java.net.MalformedURLException;
4
import javax.servlet.http.HttpServletRequest;
5
6
public final class ApplicationPath {
7
	//定义应用主机上文件路径,应用请求路径
8
	public static String appFilePath, appRequestPath;
9
	
10
	/******************************************************************
11
	 * 获取应用主机上的文件路径(/x/xx/xxx)
12
	 * @return
13
	 * @throws MalformedURLException
14
	 */
15
	public static String getFilePath(HttpServletRequest request) throws MalformedURLException{
16
		if(request == null)
17
			throw new RuntimeException("请求对象为空,无法获取应用文件路径!");
18
		
19
		if(appFilePath == null){
20
			String ServerInfo = request.getSession().getServletContext().getServerInfo();
21
			String serverType = ServerInfo.contains("Tomcat")?"T":"W";
22
			
23
			if(serverType.equals("T")){
24
				appFilePath = request.getSession().getServletContext().getRealPath("/");
25
				
26
			}else{
27
				appFilePath = request.getSession().getServletContext().getResource("/").getPath();
28
			}
29
		}
30
		return appFilePath;
31
	}
32
	/*****************************************************************
33
	 * 获取应用的WEB访问路径(http://x.xx.xxx:aaa/application)
34
	 * @return
35
	 * @throws MalformedURLException
36
	 */
37
	public static String getRequestPath(HttpServletRequest request) throws MalformedURLException{
38
		if(request == null)
39
			throw new RuntimeException("请求对象为空,无法获取应用WEB请求路径!");
40
		
41
		if(appRequestPath == null){
42
			appRequestPath = request.getScheme()
43
					+ "://" 
44
					+ request.getServerName()
45
					+ ":" 
46
					+ request.getServerPort()
47
					+ request.getContextPath()
48
					+ "/";
49
50
		}
51
		return appRequestPath;
52
	}
53
	
54
	/**私有化构造器*/
55
	private ApplicationPath(){}
56
}

+ 12 - 0
display-server/web/biz/js/plugin/fileupload.js

@ -0,0 +1,12 @@
1
require(["domReady!","wadeMobile", "util"], function(doc,WadeMobile) {
2
	//建表
3
	$("#location").tap(function() {
4
		var uploadFiles = new Array();
5
//		uploadFiles.push('/Users/kevin/Downloads/cat.png');
6
		uploadFiles.push('/Users/kevin/Downloads/cat.txt');
7
		
8
		WadeMobile.uploadFile(uploadFiles, function(){
9
//			alert("上传成功!");
10
		});
11
	});
12
});

+ 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>

+ 41 - 0
display-server/web/template/webapp/plugin/FileUpload.html

@ -0,0 +1,41 @@
1
<!DOCTYPE HTML>
2
<html class="s_bs">
3
<head>
4
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
	<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1" />
6
	<title>文件上传/下载</title>
7
	{%>template/common/Head.html%}
8
	<script type="text/javascript" src="biz/js/plugin/fileupload.js"></script>
9
	<link href="biz/css/project.css" rel="stylesheet" type="text/css" />
10
</head>
11
<body>
12

13
<div class="c_navBar">
14
	<div class="left">
15
		<div class="back">
16
			<span class="e_ico-back"></span>
17
			<span class="text">文件上传</span>
18
		</div>
19
	</div>
20
</div>
21

22
<div class="m_content m_content-nofooter" id="content" >
23
<div>
24
	<div class="c_title">文件位置</div>
25
	<div class="c_list">
26
		<ul>
27
			<li>
28
				<div class="content">
29
					<div class="main">
30
						<div class="title">文件路径</div>
31
						<div class="info" id="fileLocation">请选择上传文件路径</div>
32
					</div>
33
					<div class="fn fn-2" id="location">文件路径</div>
34
				</div>
35
			</li>
36
		</ul>
37
	</div>
38
</div>
39
</div>
40
</body>
41
</html>