上传图片 图片服务器
?
?
注意JSP提交的时候加上: ?
?
public static String parserto(String contentString,String pathString,String httpurl) {String stringBuffer=null;Date date = new Date();SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssms"); try {List list = new ArrayList();Parser parser = Parser.createParser(contentString, "UTF-8");NodeList nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {public boolean accept(Node node) {if (node instanceof ImageTag)// <img>标记return true;return false;}});if(nodeList.size()>0){for (int i = 0; i < nodeList.size(); i++) {String url = ((ImageTag) nodeList.elementAt(i)).getImageURL();url=StrUtils.htm2txt(url); Random ran = new Random();int a = ran.nextInt(1000);String imgname = simpleDateFormat.format(new Date())+a+".jpg";String reurl = ((ImageTag) nodeList.elementAt(i)).getImageURL();contentString=contentString.replace(reurl, httpurl + imgname); // pathString = "C:\\Documents and Settings\\Administrator\\桌面\\t\\";writeimg(url,pathString+imgname);}}} catch (Exception e) {}contentString.replaceAll(" ", "");return contentString;}//httpclient模拟器写图片,几乎所有格式的图片都可以,ftp方式的图片? http !public static void writeimg(String url,String targetPath) throws ClientProtocolException, IOException {HttpClient httpclient = new DefaultHttpClient();HttpGet httpget = new HttpGet(url);httpget.setHeader("referer", "http://www.baidu.com/");HttpResponse response = httpclient.execute(httpget);HttpEntity entity = response.getEntity();if (entity != null) {InputStream in = entity.getContent();int b = 0;FileOutputStream out = new FileOutputStream(new File(targetPath));while ((b = in.read()) != -1) {out.write(b);}out.close();in.close();}httpclient.getConnectionManager().shutdown();}??
?
注意:htmlparser和httpclient需要添加相应的jar
?
?
?
?
?
?