Blame view

WebRoot/plugins/ueditor/jsp/getRemoteImage.jsp 2.51 KB
ad5081d3   孙向锦   初始化项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
      <%@ page language="java" pageEncoding="utf-8"%>
      <%@ page import="java.io.*"%>
      <%@ page import="java.net.*"%>
      <%@ page import="java.util.*"%>
      <%@ page import="ueditor.Uploader" %>
      <%
      	request.setCharacterEncoding("utf-8");
      	response.setCharacterEncoding("utf-8");
      	String url = request.getParameter("upfile");
      	String state = "远程图片抓取成功!";
      	
      	String filePath = "upload";
      	String[] arr = url.split("ue_separate_ue");
      	String[] outSrc = new String[arr.length];
      	for(int i=0;i<arr.length;i++){
  
      		//保存文件路径
      		String str = application.getRealPath(request.getServletPath());
  			File f = new File(str);
  			String savePath = f.getParent() + "/"+filePath;
      		//格式验证
      		String type = getFileType(arr[i]);
  			if(type.equals("")){
  				state = "图片类型不正确!";
  				continue;
  			}
      		String saveName = Long.toString(new Date().getTime())+type;
      		//大小验证
      		HttpURLConnection.setFollowRedirects(false); 
  		    HttpURLConnection   conn   = (HttpURLConnection) new URL(arr[i]).openConnection(); 
  		    if(conn.getContentType().indexOf("image")==-1){
  		    	state = "请求地址头不正确";
  		    	continue;
  		    }
  		    if(conn.getResponseCode() != 200){
  		    	state = "请求地址不存在!";
  		    	continue;
  		    }
              File dir = new File(savePath);
  			if (!dir.exists()) {
  				dir.mkdirs();
  			}
      		File savetoFile = new File(savePath +"/"+ saveName);
      		outSrc[i]=filePath +"/"+ saveName;
      		try {
      			InputStream is = conn.getInputStream();
      			OutputStream os = new FileOutputStream(savetoFile);
      			int b;
      			while ((b = is.read()) != -1) {
      				os.write(b);
      			}
      			os.close();
      			is.close();
      			// 这里处理 inputStream
      		} catch (Exception e) {
      			e.printStackTrace();
      			System.err.println("页面无法访问");
      		}
      	}
     	String outstr = "";
     	for(int i=0;i<outSrc.length;i++){
     		outstr+=outSrc[i]+"ue_separate_ue";
     	}
     	outstr = outstr.substring(0,outstr.lastIndexOf("ue_separate_ue"));
     	response.getWriter().print("{'url':'" + outstr + "','tip':'"+state+"','srcUrl':'" + url + "'}" );
  
      %>
      <%!
      public String getFileType(String fileName){
      	String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
      	Iterator<String> type = Arrays.asList(fileType).iterator();
      	while(type.hasNext()){
      		String t = type.next();
      		if(fileName.endsWith(t)){
      			return t;
      		}
      	}
      	return "";
      }
      %>