如何用java組裝成這樣格式的,如何用java組裝成這樣格式的文件

本文目錄一覽:

如何用java組裝成這樣格式的

這就是一個jsonObject啊,你搜索下JSONObject,就知道怎麼弄。

基本偽代碼示例如下:

JSONObject json = new JSONObject()

JSONObject tmp = new JSONObject()

tmp.put(“value”:,”你好!”);

tmp.put(“color”: “#173177”);

json.put(“first”,tmp); 

system.out.print(json.toString()) ;

/* 輸出結果

{

“first”: {

“value”: “你好!”,

“color”: “#173177”

}}

*/

按照上面的,多加幾個,就是能得到你想要的。

方法二:

String jsonStr=”xxxxx” ; //就是你題目中的那長串

JSONObject json = JSonObject.fromString(jsonStr);

用java怎麼將word文檔轉成圖片格式

使用 java 操作 openoffice 實現格式轉換 辛苦了幾天。 不敢獨享成果

首先,要安裝 openoffice (廢話- -)

創建JAVA項目的時候 需要至少以下4個包

juh.jar,jurt.jar,ridl.jar,unoil.jar

以下是路徑

..\OpenOffice.org 2.3\program\classes

我只實現了我要的功能,就是把一種文件格式轉成另一種

Java代碼

package testh;

import java.io.*;

import com.sun.star.uno.UnoRuntime;

public class testcls {

public static void readdoc(String paths, String savepaths)

{

File d = new File(paths);

//取得當前文件夾下所有文件和目錄的列表

File lists[] = d.listFiles();

String pathss = new String(“”);

//對當前目錄下面所有文件進行檢索

for(int i = 0; i lists.length; i ++)

{

if(lists[i].isFile())

{

String filename = lists[i].getName();

String filetype = new String(“”);

//取得文件類型

filetype = filename.substring((filename.length() – 3), filename.length());

//判斷是否為doc文件

if(filetype.equals(“doc”))

{

System.out.println(“當前正在檢索….”);

//打印當前目錄路徑

System.out.println(paths);

//打印doc文件名

String fname=filename.substring(0, (filename.length() – 4));

System.out.println(“檢索到文件”+fname);

try

{

//指定文件路徑和名稱

String path = savepaths+fname+”.html”;

File outfilename = new File(path);

/** *//**

* 檢查文件是否存在.

* @throws IOException

*

*/

if (!outfilename.exists()) {

System.err.println(“目標路徑無同名文件,開始轉換”);

System.out.print(“正在轉換文件:”+fname);

Dump(paths,fname,savepaths);

}

else

{

System.out.print(“文件已存在,放棄創建,請處理存在文件後再運行…\n”);

continue;

}

RandomAccessFile mm = null;

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

}

}

public static void Dump(String FilePath,String FileName,String OutPath)

{

com.sun.star.uno.XComponentContext xContext = null;

try {

// get the remote office component context

xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();

System.out.println(“Connected to a running office …”);

// get the remote office service manager

com.sun.star.lang.XMultiComponentFactory xMCF =

xContext.getServiceManager();

Object oDesktop = xMCF.createInstanceWithContext(

“com.sun.star.frame.Desktop”, xContext);

com.sun.star.frame.XComponentLoader xCompLoader =

(com.sun.star.frame.XComponentLoader)

UnoRuntime.queryInterface(

com.sun.star.frame.XComponentLoader.class, oDesktop);

java.io.File sourceFile = new java.io.File(FilePath+FileName+”.doc”);//讀取的文件

StringBuffer sLoadUrl = new StringBuffer(“”);

sLoadUrl.append(sourceFile.getCanonicalPath().replace(‘\\’, ‘/’));

sourceFile = new java.io.File(OutPath+FileName+”.html”);//輸出的文件

System.out.print(OutPath+” “+FileName);

StringBuffer sSaveUrl = new StringBuffer(“”);

sSaveUrl.append(sourceFile.getCanonicalPath().replace(‘\\’, ‘/’));

com.sun.star.beans.PropertyValue[] propertyValue =

new com.sun.star.beans.PropertyValue[1];

propertyValue[0] = new com.sun.star.beans.PropertyValue();

propertyValue[0].Name = “Hidden”;

propertyValue[0].Value = new Boolean(true);

Object oDocToStore = xCompLoader.loadComponentFromURL(

sLoadUrl.toString(), “_blank”, 0, propertyValue );

com.sun.star.frame.XStorable xStorable =

(com.sun.star.frame.XStorable)UnoRuntime.queryInterface(

com.sun.star.frame.XStorable.class, oDocToStore );

propertyValue = new com.sun.star.beans.PropertyValue[ 2 ];

propertyValue[0] = new com.sun.star.beans.PropertyValue();

propertyValue[0].Name = “Overwrite”;

propertyValue[0].Value = new Boolean(true);

propertyValue[1] = new com.sun.star.beans.PropertyValue();

propertyValue[1].Name = “FilterName”;

propertyValue[1].Value = “HTML (StarWriter)”;//你一定發現了,把這裡改成其他參數,可以保存為不同的文件 MS Word 97,writer_pdf_Export

xStorable.storeAsURL( sSaveUrl.toString(), propertyValue );

System.out.println(“\nDocument \”” + sLoadUrl + “\” saved under \”” +

sSaveUrl + “\”\n”);

com.sun.star.util.XCloseable xCloseable = (com.sun.star.util.XCloseable)

UnoRuntime.queryInterface(com.sun.star.util.XCloseable.class,

oDocToStore );

if (xCloseable != null ) {

xCloseable.close(false);

} else

{

com.sun.star.lang.XComponent xComp = (com.sun.star.lang.XComponent)

UnoRuntime.queryInterface(

com.sun.star.lang.XComponent.class, oDocToStore );

xComp.dispose();

}

System.out.println(“document closed!”);

}

catch( Exception e ) {

e.printStackTrace(System.err);

System.exit(1);

}

}

public static void main(String args[]) throws Exception

{

String paths = new String(“c:\\a\\”);

String savepaths = new String (“f:\\”);

readdoc(paths,savepaths);

}

}

如何用JAVA轉換圖像格式

關於圖像轉換的方式,實際上操作的是圖像的位元組流。我的工作中遇到過將bmp文件壓縮為jpg以便於網絡傳輸的課題。所以我這裡重點介紹bmp轉為jpg的一個方法。

實際上,我更喜歡使用以前sun公司內部使用的api提供的轉換方法,這裡使用到了兩個很重要的類:

com.sun.image.codec.jpeg.JPEGCodec

com.sun.image.codec.jpeg.JPEGImageEncoder

需要注意的是,它們所屬的一個jar包不存在於編譯目錄下,但存在於運行目錄下,所以我們首先需要在jre文件下找到rt.jar並導入進來以使得編譯通過。

我改寫了網上的一個轉換代碼,所得代碼如下:

此外,原sun公司開源的jar包jai_corec_1.1.3.jar也提供了圖片格式的轉碼方式,這裡也提供了轉碼方式,僅供參考:

備註:親自嘗試,當從jpg轉bmp時會轉很久很久時間(看不到盡頭),轉得的文件可以很大,所以建議不要使用。

如何用java轉換圖像格式為jpg

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

public class ImageFormat {

public static void main(String[] args) {

File file = new File(“c:\\test.jpg”);

changFormat(file, “png”, new File(“c:\\test.png”));// 轉為png

changFormat(file, “bmp”, new File(“c:\\test.bmp”));// 轉為bmp

//changFormat(file, “jpeg”, new File(“c:\\test.jpg”));// 轉為jpg

changFormat(file, “gif”, new File(“c:\\test.gif”));// 轉為gif

}

//第一個參數 原圖的File對象 第二個參數 目標格式 第三個參數 輸出圖像的File對象

public static void changFormat(File srcFile, String format, File formatFile) {

try {

BufferedImage srcImg = ImageIO.read(srcFile);// 讀取原圖

ImageIO.write(srcImg, format, formatFile);// 用指定格式輸出到指定文件

} catch (IOException e) {

e.printStackTrace();

}

}

}

java組裝複雜的json用什麼包怎麼用

XSTREAM 可以直接把對象轉成json字符串。

具體用法百度很多的。

當然也可以用json-lib 用法如下 user是一個對象

User user=new User();

user.setId(“123”);

user.setName(“JSONServlet”);

user.setPassword(“JSON”);

user.setSay(“Hello , i am a servlet !”);

JSONObject json=new JSONObject();

json.accumulate(“success”, true);

json.accumulate(“user”, user);

原創文章,作者:GROTV,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/315881.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
GROTV的頭像GROTV
上一篇 2025-01-09 12:14
下一篇 2025-01-09 12:14

相關推薦

發表回復

登錄後才能評論