亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

java圖片處理和jdom的使用

系統 1754 0

????? 本人聯系方式:msn : snowfox_1028@hotmail.com ? email:snowfox_1028@163.com

?????? 學習和使用這么久java,但是還是覺得很茫然,什么都知道一點,但是什么都不精通,如struts ,hibernate ,ejb,ibatis,spring,現在想想,應該選擇一個目標,進行深入的學習和應用,這樣才能有所成,做了這么長的java開發,最后得到的結論和經驗就是這些。也好,有了一個明確的方向,一個目標,呵呵,可以走我自己的路了,日積月累,總會有所成的。現在寫下這個帖子,是想感謝那些經常發帖的人,因為我每次遇到問題,總是有一些帖子給我啟發,發帖的這些人應該是高尚的,值得學習,今天效仿一次,勿見笑

簡要介紹這幾個類的作用:XmlTest.java解析xml文件使用的是jdom(獲取xml文件時請注意路徑的獲取),ImageTest.java使用讀寫圖片的,包括遠程服務器的圖片,TitleService.java這是一個servlet,實現將Image對象寫到頁面上。以下幾個類實現功能是從服務器上獲取圖片放到本地緩存上,并寫到頁面上,當下次從默認路徑讀取圖片時,如果本地緩存有,則從本地緩存讀取,否則從服務器讀取。調用servlet方式是: http://localhost:8080/DemoTitleService/TitleService?X=264&Y=115&LAYERS=hyns:a61030_region&L=0 ,其中layers這個參數名需要在web.net.xml配置。

請注意圖片的類型,png,jpeg,gif這三種格式在下面程序的使用。

如下:

圖片讀寫類

package tool;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
?/**
? * @param args
? * @author?****
? * @date 2007-07-16
? */
public class ImageTest {

?// get image from local
?public BufferedImage readImage(String path_name){
??File file = new File(path_name); // 讀入文件
??try {
???BufferedImage img = ImageIO.read(file);// 構造Image對象
???System.out.println(" get local image successfully ! ");
???return img;
??} catch (IOException e) {
???e.printStackTrace();
???return null;
??} ??
?}
?// get image from server
?public List readImageFromServer (String HTTPurl){
??
??try {
???java.net.URL imageURL = new java.net.URL(HTTPurl);
???BufferedImage img = ImageIO.read(imageURL);
???InputStream is = imageURL.openStream();
???List imageInfo = null;
???if(img != null){
????imageInfo = new ArrayList();
????imageInfo.add(img);
????imageInfo.add(is);
????return imageInfo;
???}else{
????System.out.println(" get server image failure ! ");
????return null;
???}
??} catch (Exception e) {
???e.printStackTrace();???
???return null;
??}
?}
?// write image to local
??? public boolean writeImage(InputStream is, String filepath,String filename){
??? ?int b = -1 ;
??try {
???File outfile = new File(filepath); //如果沒有創建新的文件夾
???if(! outfile.isDirectory()){
????outfile.mkdirs();
???}
???if(! filepath.endsWith("/")){
????filepath = filepath +"/" ;
???}

???FileOutputStream fos = new FileOutputStream(filepath+filename);// 輸出到文件流
???
???while((b = is.read())!= -1){
????fos.write(b);
???}
???fos.close();
???is.close();
???System.out.println(" write image to local successfully ! ");
??} catch (Exception e) {
???e.printStackTrace();
???return false;
??}
??? ?return true;
??? }?
}

?xml解析類

package tool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
?/**
? * @param args
? * @throws IOException
? * @throws JDOMException
? * @author?*****
? * @date 2007-07-16
? */
public class XmlTest {
?
?public static String[] loadXml(String filepath){
??try {
???java.io.FileInputStream? xmlFile = new java.io.FileInputStream(filepath);
???SAXBuilder? builder = new SAXBuilder();
???Document doc = builder.build(xmlFile);
???Element config = doc.getRootElement();
???List application = (List) config.getChildren("appSettings");
???Element e1 = (Element) application.get(0);
???
???String[]? array =new String[4];
???array[0] = e1.getChildText("cachePath");
???array[1] = e1.getChildText("LZD");
???array[2] = e1.getChildText("wmsURL");
???array[3] = e1.getChildText("param");
???System.out.println("parse web.net.xml successfully ! ");
???return array;
??} catch (Exception e) {
???e.printStackTrace();//
???return null;
??}
?}?
}

工具類

package tool;

import java.awt.Image;
import java.awt.image.BufferedImage;
?/**
? * @param args
? * @author? ****
? * @date 2007-07-16
? */
public class UtilTest {
? //number format to "0000"
?public static String strFormat(String str){
??if(str.length()==1){
???str = "000" +str;
??}else if(str.length()==2){
???str = "00" +str;
??}if(str.length()==3){
???str = "0" +str;
??}
??return str;
?}
?//from image to bufferedimage
?public static java.awt.image.BufferedImage fromImageTOBufferimage(Image image){
??BufferedImage tag = new BufferedImage(image.getWidth(null) , image.getHeight(null),
????BufferedImage.TYPE_INT_RGB);
??tag.getGraphics().drawImage(image, 0, 0, image.getWidth(null) , image.getHeight(null) , null);
??System.out.println("change from image to bufferedimage successfully ") ;
??return tag;
?}
?
?//String split
?public static String[] strSplit(String str){
??String[] strArray = str.split(":");
??return strArray;
?}
}

servlet類

package servletTest;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tool.ImageTest;
import tool.UtilTest;
import tool.XmlTest;

public class TitleService extends HttpServlet {
??? /*
???? * @author?*****
???? * @date?? 2007-07-16
???? * */

?public void doGet(HttpServletRequest request, HttpServletResponse response)
???throws ServletException, IOException {
??
/*****************************************************/
?
??String[] strName ;
???? int intLevel;
???? String strForder = "";
???? String strPicName = "";
???? String[] strType ={ "jpg", "jpeg", "png"};
???? String strPath;
???? String strFile;
???? Integer X;
???? Integer Y;
???? /*? 從配置文件web.net.xml中讀取數據? */
???? double LZD ;//
???? String cathPath;
???? String wmsURL ;
???? String param;
??????
???? String xmlpath = request.getRealPath("/");
??????? String[] arrxml = XmlTest.loadXml(xmlpath+"Web.net.xml");
??????? cathPath = arrxml[0];
??????? LZD = Double.parseDouble(arrxml[1]);
??????? wmsURL = arrxml[2];
??????? param? = arrxml[3];
???? //從servlet參數獲取值
??????? String paramvalue = request.getParameter(param.trim()) ;
???? strName = UtilTest.strSplit(paramvalue);
??????? intLevel = Integer.parseInt(request.getParameter("L"));
??????? X = Integer.parseInt(request.getParameter("X"));
??????? Y = Integer.parseInt(request.getParameter("Y"));
???????????????
??????? String temp_strPicName = "";
???????
??????? boolean isRightLevel;
??????? int i = 0;
??????? /* 圖片路徑信息處理? */
??????? try{
??????? ?if(Y.toString().length()<= 4){
??????? ??String str_y = Y.toString();
??????? ??String str_x = X.toString();
??????? ??str_y = UtilTest.strFormat(str_y);
??????? ??str_x = UtilTest.strFormat(str_x);
???????
??????? ??strForder = str_y;
??????? ??strPicName = strForder + "_" + str_x;
??????????? }else{
??????????? ? strForder = Y.toString();
???????????????? strPicName = X.toString();
???????????????? temp_strPicName = strForder + "_" + strPicName;
??????????? }
??????? }catch(java.lang.Exception e){
??????? ?response.setContentType("text/plain");
??????? ?java.io.PrintWriter out = response.getWriter();
??????? ?out.println("404? File Not Found");
??????? ?out.close();
??????? }
???????
??????? if(! cathPath.endsWith("/")){
??????? ?cathPath = cathPath + "/";
??????? }
??????
??? ?strPath = cathPath + strName[0]+"/"+ strName[1] + "/" + intLevel + "/" + strForder + "/"; //E:/數據/廣州數據--緩存/T/L/Y/Y_X
??????? strFile = strPath + strPicName + "." + strType[0];
???????
??????? if (intLevel >= 0 && intLevel <= 12){
??????????? isRightLevel = true;
??????? }
??????? else{
??????????? isRightLevel = false;
??????? }
??????? /*? 判斷圖片是否存在本地緩存上???? */
??????? File readFile = new File(strFile);
??????? while(! readFile.isFile()&& i < strType.length - 1){
??????? ?i++;
???????? strFile = strPath + strPicName + "." + strType[i];
???????? readFile = new File(strFile);
??????? }
???????
??????? if(! readFile.isFile()){
??????? ?strFile = strPath + temp_strPicName + "." + strType[0];
??????????? i = 0;
??????????? while (! readFile.isFile() && i < strType.length - 1){
??????????????? i++;
??????????????? strFile = strPath + temp_strPicName + "." + strType[i];
??????????????? readFile = new File(strFile);
??????????? }
??????? }
??????? /* 如果在本地緩存中有該圖片,那么寫到頁面上? */
??????? if(readFile.isFile() && isRightLevel ){
??????? ?try{
??????? ??ImageTest image = new ImageTest();
??????????? ?response.setContentType("image/png");
??????? ??ServletOutputStream sos = response.getOutputStream();
??????? ??BufferedImage bi= image.readImage(strFile);
??????? ??ImageIO.write(bi,"png",sos);
??????? ??sos.close();
??????? ?}catch(java.lang.Exception e){
??????? ??response.setContentType("text/plain");
??????????? ?java.io.PrintWriter out = response.getWriter();
??????????? ?out.println("404? File Not Found");
??????????? ?out.close();
??????? ?}?
??????? }else if (isRightLevel){
??????????? try{
??????????????? /*如果本地緩存沒有該圖片 ,那么從指定的服務器上讀取該圖片*/
??????????? ? String strLatitudeX1;
???????????????? String strLatitudeX2;
???????????????? String strLongitudeY1;
???????????????? String strLongitudeY2;
???????????????? String strWMSOtherParas;
???????????????? String strHttpUrl;
????????????????
???????????????? double dblUnit;
???????????????? dblUnit = LZD / (1 << intLevel);
????????????????// BigDecimal是用來計算浮點型數據
???????????????? BigDecimal bigX1 = new BigDecimal(Y).multiply(new BigDecimal(dblUnit)) ;
???????????????? bigX1 = bigX1.add(new BigDecimal(-90)) ;
???????????????? strLatitudeX1 = new String(bigX1.setScale(10,1).toString());
????????????????
???????????????? BigDecimal bigX2 = new BigDecimal(Y+1).multiply(new BigDecimal(dblUnit)) ;
???????????????? bigX2 = bigX2.add(new BigDecimal(-90)) ;
???????????????? strLatitudeX2 = new String(bigX2.setScale(10,1).toString());
????????????????
???????????????? BigDecimal bigY1 = new BigDecimal(X).multiply(new BigDecimal(dblUnit)) ;
???????????????? bigY1 = bigY1.add(new BigDecimal(-180)) ;
???????????????? strLongitudeY1 = new String(bigY1.setScale(10,1).toString());
????????????????
???????????????? BigDecimal bigY2 = new BigDecimal(X+1).multiply(new BigDecimal(dblUnit)) ;
???????????????? bigY2 = bigY2.add(new BigDecimal(-180)) ;
???????????????? strLongitudeY2 = new String(bigY2.setScale(10,1).toString());
????????????????
???????????????? System.out.println(strLatitudeX1+strLatitudeX2+strLongitudeY1 +strLongitudeY2);
//?????????????? 從服務器上獲取圖片
???????????????? strHttpUrl = wmsURL + "&"+param+"="+paramvalue+"&BBOX=";
???????????????? strHttpUrl = strHttpUrl + strLongitudeY1 + "," + strLatitudeX1 + "," + strLongitudeY2 + "," + strLatitudeX2 ;
???????????????? System.out.println("server url : "+strHttpUrl);
???????????????? ImageTest getServerImage = new ImageTest();
???????????????? List imageInfo = getServerImage.readImageFromServer(strHttpUrl);
????????????????
???????????????? BufferedImage getImage =(BufferedImage) imageInfo.get(0);
??????????? ? String strSaveFile = strPath + strPicName + "." + strType[1];
???????????????? getServerImage.writeImage((InputStream) imageInfo.get(1), strPath , strPicName + "." + strType[1] );
??????? ?? response.setContentType("image/png");
???? ServletOutputStream sos = response.getOutputStream();
??????? ?? ImageIO.write(getImage,"png",sos);
??????? ?? sos.close();???????????????????????
??????????? }catch (Exception ex){
??????????? ?java.io.PrintWriter out = response.getWriter();
??????????? ?out.println(ex.getMessage());
??????????? ?out.close();
??????????? }
??????? }else{
??????? ?response.setContentType("text/plain");
??????? ?java.io.PrintWriter out = response.getWriter();
??????? ?out.println("404? File Not Found");
??????? ?out.close();
??????? }
?}
}

被解析的xml文件

<?xml version="1.0" encoding="gb2312"?>

<configuration>
?<appSettings>
??<cachePath>D:/temp/</cachePath>
??<LZD>1.125</LZD>
??<wmsURL><![CDATA[http://192.168.0.4:8080/geoserver/wms?FORMAT=image/gif&TRANSPARENT=TRUE&HEIGHT=406&REQUEST=GetMap&WIDTH=810&STYLES=&SRS=EPSG:4326&VERSION=1.1.1]]></wmsURL>
??<param>LAYERS</param>
?</appSettings>
</configuration>

web.xml

?<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
?xmlns=" http://java.sun.com/xml/ns/j2ee "
?xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
?xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee
? http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
? <servlet>
??? <servlet-name>TitleService</servlet-name>
??? <servlet-class>servletTest.TitleService</servlet-class>
? </servlet>

? <servlet-mapping>
??? <servlet-name>TitleService</servlet-name>
??? <url-pattern>/TitleService</url-pattern>
? </servlet-mapping>
</web-app>

java圖片處理和jdom的使用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲综合免费视频 | 国产精品所毛片视频 | 中文字幕在线永久 | 亚洲精品www久久久久久久软件 | 国内精品伊人久久久影院 | 中文字幕在线二区 | 亚洲综合激情九月婷婷 | 五月久久婷婷综合片丁香花 | 夜夜艹日日艹 | 中文字幕在线视频观看 | 日韩女同视频 | 全部免费的毛片在线看青青 | 黄页网址大全免费观看美女 | 国产精品久久久久久久9999 | 日夜操在线视频 | 国产黄片毛片 | 国产资源精品一区二区免费 | 国产精品自在欧美一区 | aaa级大片 | 奇米影视奇米色777欧美 | 99久久香蕉国产线看观香 | 久久大香香蕉国产免费网站 | 2021国产精品自产拍在线观看 | 久久综合九色综合欧美狠狠 | 亚洲国产精品欧美日韩一区二区 | 日本亚洲成高清一区二区三区 | 欧美国产日韩在线 | 色综合中文字幕在线亚洲 | 在线观看福利影院 | 在线精品视频成人网 | 国产精品久久久久久久福利院 | 国产成人亚洲精品老王 | 在线观看中文字幕一区 | 国产一区曰韩二区欧美三区 | 天堂一区二区三区在线观看 | 国产男女爱视频在线观看 | 久久99蜜桃精品久久久久小说 | 欧洲色片 | 久久这里只有精品66 | 神马影院我不卡手机版 | 日本精品在线 |