package com.slhe.grad;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping({"/img"})
public class GradImg {
private static final String SAVE_PATH = "D:/img/";
@RequestMapping(value="",method = RequestMethod.GET)
public ModelAndView img(){
return new ModelAndView("index");
}
@RequestMapping(value="/grad",method = RequestMethod.POST)
public ModelAndView grad(HttpServletRequest request) throws Exception{
String urlPath = request.getParameter("imgurl");
String useforntExt = request.getParameter("useforntext");
String forntExt = request.getParameter("forntext");
String imgStr = autosaveimg(urlPath,useforntExt==null?false:true,forntExt);
ModelAndView mav = new ModelAndView();
mav.addObject("imgstr", imgStr);
mav.setViewName("index");
return mav;
}
private String autosaveimg(String urlPath,boolean useforntext,String forntext) throws Exception{
if(urlPath==null || urlPath.trim().length() ==0)
return "";
URL url = new URL(urlPath);
InputStreamReader isr = new InputStreamReader(url.openStream(),"utf-8");
BufferedReader br = new BufferedReader(isr);
String reg="(?i).+?\\.(jpg)";
GroupMethod gMethod = new GroupMethod();
StringBuffer buffer = new StringBuffer();
String strRead = "";
while((strRead = br.readLine())!=null ){
String strGet = gMethod.regularGroup(reg, strRead);
if(!strGet.equals("")){
strGet = strGet.replace("\\/", "/");
buffer.append(strGet).append("<br>");
if(useforntext){
autosave(forntext + strGet);
}else
autosave(strGet);
}
}
br.close();
isr.close();
return buffer.toString();
}
private boolean autosave(String imgUrl){
boolean b = false;
try {
imgUrl = imgUrl.replace("https", "http");
URL url = new URL(imgUrl);
DataInputStream is = new DataInputStream(url.openStream());
FileOutputStream os = new FileOutputStream(new File(SAVE_PATH+new Random().nextInt(1000000)+".jpg"));
byte[] buffer = new byte[1024];
int length=0;
while((length = is.read(buffer)) >0){
os.write(buffer);
Thread.sleep(20);
}
is.close();
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
javaWeb应用,需要布署在tomcat下 操作步骤: 1 解压GradImg.rar 2 把项目导入Eclipse 或 MyEclipse 进行编译 3 布署到tomcat下 4 启用应用输入http://127.0.0.1:8080/GradImg,跳转到操作页面 5 图片抓取后存放路径为:D:/img 需要先在D盘下创建img文件夹
资源推荐
资源详情
资源评论
收起资源包目录
GradImg.rar (42个子文件)
GradImg
.project 2KB
.springBeans 473B
.mymetadata 294B
src
com
slhe
grad
GroupMethod.java 649B
GradImg.java 3KB
log4j.properties 792B
WebRoot
WEB-INF
spring-form.tld 69KB
springmvc-servlet.xml 2KB
jsp
index.jsp 2KB
spring.tld 17KB
classes
com
slhe
grad
GradImg.class 4KB
GroupMethod.class 1KB
log4j.properties 792B
lib
org.springframework.beans-3.0.5.RELEASE.jar 542KB
org.springframework.expression-3.0.5.RELEASE.jar 166KB
jstl-1.2.jar 405KB
org.springframework.web.struts-3.0.5.RELEASE.jar 31KB
com.springsource.org.apache.commons.httpclient-3.1.0.jar 301KB
org.springframework.asm-3.0.5.RELEASE.jar 52KB
com.springsource.org.codehaus.jackson.mapper-1.0.0.jar 264KB
org.springframework.oxm-3.0.5.RELEASE.jar 60KB
org.springframework.web.portlet-3.0.5.RELEASE.jar 171KB
jsf-impl.jar 1.15MB
org.springframework.web.servlet-3.0.5.RELEASE.jar 409KB
jsf-api.jar 314KB
org.springframework.web-3.0.5.RELEASE.jar 386KB
com.springsource.org.apache.commons.fileupload-1.2.0.jar 53KB
org.springframework.context-3.0.5.RELEASE.jar 653KB
org.springframework.core-3.0.5.RELEASE.jar 373KB
com.springsource.org.apache.commons.logging-1.1.1.jar 60KB
com.springsource.org.apache.log4j-1.2.15.jar 387KB
web.xml 2KB
img 0B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.core.resources.prefs 88B
org.eclipse.wst.common.component 459B
.jsdtscope 500B
.classpath 656B
共 42 条
- 1
资源评论
slhe08
- 粉丝: 1
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功