浏览代码

提交内容:
下载图片的功能完善

chengwb3 9 年之前
父节点
当前提交
10d925f980

+ 43 - 17
display-server/src/com/ai/server/bean/UploadDownloadBean.java

@ -142,7 +142,7 @@ public class UploadDownloadBean extends DisplayBean {
142 142
	 * @return
143 143
	 * @throws Exception
144 144
	 */
145
	public IData download(IData param) throws Exception{
145
	/*public IData download(IData param) throws Exception{
146 146
		HttpServletRequest request = ServletManager.getRequest();
147 147
		HttpServletResponse response = ServletManager.getResponse();
148 148
		//获取文件需要上传到的路径
@ -175,7 +175,7 @@ public class UploadDownloadBean extends DisplayBean {
175 175
        response.flushBuffer();
176 176
		
177 177
		return null;
178
	}
178
	}*/
179 179
	
180 180
	public IData upload(IData param) throws Exception{
181 181
	    IData returnMap = new DataMap();  // 返回的对象
@ -194,7 +194,7 @@ public class UploadDownloadBean extends DisplayBean {
194 194
        int random = (int)(Math.random()*10000);
195 195
        String dateStr_random = dateStr+"_"+random;  //时间字符串+随机数 最后一级别的文件夹
196 196
        String path = null; // 存放数据库用 相对路径 path = typePath + dateStr_random + fileName
197
        String rootDir = ApplicationPath.getFilePath(request).replace("//", File.separator).replace("\\", File.separator) + File.separator + "upload"; // eg:F:\eclipseWorkspace3\android-share\display-server\web
197
        String rootDir = ApplicationPath.getFilePath(request).replace("//", File.separator).replace("\\", File.separator) + "upload"; // eg:F:\eclipseWorkspace3\android-share\display-server\web
198 198
        String typePath = null;
199 199
        String filePath = null;  // 上传服务器用 绝对路径 filePath = rootDir + typePath + dateStr_random + fileName
200 200
        String fileName = null;
@ -272,28 +272,21 @@ public class UploadDownloadBean extends DisplayBean {
272 272
            // TODO Auto-generated catch block
273 273
            e.printStackTrace();
274 274
        }
275
        finally
276
        {
277
            if (in != null)
278
            {
279
                try
280
                {
275
        finally {
276
            if (in != null) {
277
                try {
281 278
                    in.close();
282 279
                }
283
                catch (Exception e)
284
                {
280
                catch (Exception e) {
285 281
                    throw new RuntimeException(e);
286 282
                }
287 283

288 284
            }
289
            if (outStream != null)
290
            {
291
                try
292
                {
285
            if (outStream != null) {
286
                try {
293 287
                    outStream.close();
294 288
                }
295
                catch (Exception e)
296
                {
289
                catch (Exception e) {
297 290
                    throw new RuntimeException(e);
298 291
                }
299 292

@ -302,4 +295,37 @@ public class UploadDownloadBean extends DisplayBean {
302 295
       
303 296
        return returnMap;
304 297
    }
298
	
299
	public InputStream download(IData param) throws Exception{
300
        HttpServletRequest request = ServletManager.getRequest();
301
        HttpServletResponse response = ServletManager.getResponse();
302
        request.setCharacterEncoding("utf-8"); // 设置编码
303
        response.reset(); //清除首部的空白行。
304
        response.setContentType("text/html;charset=utf-8");
305
        String path = param.getString("FILE_PATH");
306
        String rootDir = ApplicationPath.getFilePath(request).replace("//", File.separator).replace("\\", File.separator) + "upload"; // eg:F:\eclipseWorkspace3\android-share\display-server\web
307
        //获取文件地址
308
        String filePath = FileUtil.connectFilePath(rootDir, path);
309
        File file = new File(filePath);
310
        if(!file.isFile()) {
311
            throw new RuntimeException("上传的文件不存在,请检查文件路径!");
312
        }
313

314
        InputStream in = new FileInputStream(file);
315
        
316
        int length = 0;
317
        byte[] buf = new byte[1024];
318
        //写出到文件中
319
        OutputStream out = response.getOutputStream();
320
        while((length = in.read(buf) ) != -1) {
321
            //在BUF数组中取出数据写到(输出流)磁盘上
322
            out.write(buf, 0, length);
323
        }
324
        out.flush();
325
        in.close();
326
        out.close();
327
        response.flushBuffer();
328
        
329
        return in;
330
    }
305 331
}

+ 5 - 4
display-server/web/biz/js/plugin/uploaddownload.js

@ -4,13 +4,14 @@ require(["domReady!","wadeMobile", "util"], function(doc,WadeMobile) {
4 4
		/*单文件上传*/
5 5
		WadeMobile.getPicture(function(filePath){
6 6
			var params = Wade.DataMap();
7
			params.put("UPLOAD_PATH","uploadPhoto"); // 图片上传的制定相对路径,如果没有设置,默认值temp
8
//			params.put("FILE_NAME","my.png");   // 图片默认的名字,如果没有设置,取原图片的名字
7
			params.put("UPLOAD_PATH","photo"); // 图片上传的制定相对路径,如果没有设置,默认值temp
8
			params.put("FILE_NAME","my.png");   // 图片默认的名字,如果没有设置,取原图片的名字
9 9
			//服务端SESSION_ID不能为空,服务端配置verify="false"并不校验
10 10
//			params.put("SESSION_ID",new Date().getTime());
11 11
			WadeMobile.uploadWithServlet(filePath, "UploadDownloadBean.upload", params.toString(), function(result){
12 12
				var data = new Wade.DataMap(result);
13
				alert("上传文件的大小:" + data.get("size") + "字节.");
13
				alert(result);
14
				alert("上传文件的大小:" + data.get("size") + "字节,文件路径:" + data.get("lujing"));
14 15
				$("#fileUpload").html(data.get("lujing"));
15 16
			});
16 17
		});
@ -20,7 +21,7 @@ require(["domReady!","wadeMobile", "util"], function(doc,WadeMobile) {
20 21
	//下载
21 22
	$("#download").tap(function() {
22 23
		//文件保存路径
23
		var savePath = "temp/my.png";
24
		var savePath = "/my123.png";
24 25
		
25 26
		var params = new Wade.DataMap();
26 27
		params.put("FILE_PATH", "photo/my.png");//下载文件的相对路径

+ 1 - 1
wade-mobile-func/src/com/wade/mobile/func/MobileNetWork.java

@ -377,7 +377,7 @@ public class MobileNetWork extends Plugin {
377 377
				String result = null;
378 378
				try{
379 379
					InputStream in = UnirestUtil.downloadByPost(MobileConfig.getInstance().getRequestUrl(), postData);
380
					FileUtil.writeFile(in, savePath);
380
					FileUtil.writeFile(in, "/sdcard" + savePath);
381 381
					result = "下载成功";
382 382
				}catch(Exception e){
383 383
					MobileNetWork.this.error(savePath + "异常:"+e.getMessage());// 报错回调