19.thumbHeight=(int)(thumbWidth/imageRatio);
20.}else{
21.thumbWidth=(int)(thumbHeight*imageRatio);
22.}
23.
24.//draworiginalimagetothumbnailimageobjectand
25.//scaleittothenewsizeon-the-fly
26.BufferedImagethumbImage=newBufferedImage(thumbWidth,
27.
28.thumbHeight,BufferedImage.TYPE_INT_RGB);
29.Graphics2Dgraphics2D=thumbImage.createGraphics();
30.graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
31.
32.RenderingHints.VALUE_INTERPOLATION_BILINEAR);
33.graphics2D.drawImage(image,0,0,thumbWidth,thumbHeight,null);
34.
35.//savethumbnailimagetooutFilename
36.BufferedOutputStreamout=newBufferedOutputStream(new
37.
38.FileOutputStream(outFilename));
39.JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
40.JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam
41.
42.(thumbImage);
43.quality=Math.max(0,Math.min(quality,100));
44.param.setQuality((float)quality/100.0f,false);
45.encoder.setJPEGEncodeParam(param);
46.encoder.encode(thumbImage);
47.out.close();
48.}
复制代码
9.在Java中创建JSON数据
1.Readthisarticleformoredetails.
2.DownloadJARfilejson
3.-rpc-1.0.jar(75kb)
4.importorg.json.JSONObject;
5....
6....
7.JSONObjectjson=newJSONObject();
8.json.put("city","Mumbai");
9.json.put("country","India");
10....
11.Stringoutput=json.toString();
12....
复制代码
10.在Java中使用iTextJAR打开PDF
1.Readthisarticleformoredetails.
2.
3.importjava.io.File;
4.importjava.io.FileOutputStream;
5.importjava.io.OutputStream;
6.importjava.util.Date;
7.
8.importcom.lowagie.text.Document;
9.importcom.lowagie.text.Paragraph;
10.importcom.lowagie.text.pdf.PdfWriter;
11.
12.publicclassGeneratePDF{
13.
14.publicstaticvoidmain(String[]args){
15.try{
16.OutputStreamfile=newFileOutputStream(newFile("C:
\\Test.pdf"));
17.
18.Documentdocument=newDocument();
19.PdfWriter.getInstance(document,file);
20.document.open();
21.document.add(newParagraph("HelloKiran"));
22.document.add(newParagraph(newDate().toString()));
23.
24.document.close();
25.file.close();
26.
27.}catch(Exceptione){
28.
29.e.printStackTrace();
30.}
31.}
32.}
复制代码
11.在Java上的HTTP代理设置
1.System.getProperties().put("http.proxyHost","someProxyURL");
2.System.getProperties().put("http.proxyPort","someProxyPort");
3.System.getProperties().put("http.proxyUser","someUserName");
4.System.getProperties().put("http.proxyPassword","somePassword");
复制代码
12.JavaSingleton例子
1.Readthisarticleformore
2.details.
3.Update:
ThanksMarkusforthecomment.Ihaveupdatedthecodeand
4.changeditto
5.morerobustimplementation.
6.
7.
8.publicclassSimpleSingleton{
9.privatestaticSimpleSingletonsingleInstance= newSimpleSingleton();
10.
11.//Markingdefaultconstructorprivate
12.//toavoiddirectinstantiation.
13.privateSimpleSingleton(){
14.}
15.
16.//GetinstanceforclassSimpleSingleton
17.publicstaticSimpleSingletongetInstance(){
18.
19.returnsingleInstance;
20.}
21.}
22.OnemoreimplementationofSingletonclass.ThankstoRalphandLukaszZielinski
23.
24.forpointingthisout.
25.
26.
27.publicenumSimpleSingleton{
28.INSTANCE;
29.publicvoiddoSomething(){
30.}
31.}
32.
33.//CallthemethodfromSingleton:
34.SimpleSingleton.INSTANCE.doSomething();
复制代码
13.在Java上做屏幕截图
1.Readthisarticleformoredetails.
2.
3.
4.importjava.awt.Dimension;
5.importjava.awt.Rectangle;
6.importjava.awt.Robot;
7.importjava.awt.Toolkit;
8.importjava.awt.image.BufferedImage;
9.importjavax.imageio.ImageIO;
10.importjava.io.File;
11.
12....
13.
14.publicvoidcaptureScreen(StringfileName)throwsException{
15.
16.DimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();
17.RectanglescreenRectangle=newRectangle(screenSize);
18.Robotrobot=newRobot();
19.BufferedImageimage=robot.createScreenCapture(screenRectangle);
20.ImageIO.write(image,"png",newFile(fileName));
21.
22.}
23....
复制代码
14.在Java中的文件,目录列表
1.Filedir=newFile("directoryName");
2.String[]children=dir.list();
3.if(children==null){
4.//Eitherdirdoesnotexistorisnotadirectory
5.}else{
6.for(inti=0;i7.//Getfilenameoffileordirectory
8.Stringfilename=children;
9.}
10.}
11.
12.//Itisalsopossibletofilterthelistofreturnedfiles.
13.//Thisexampledoesnotreturnanyfilesthatstartwith`.'.
14.FilenameFilterfilter=newFilenameFilter(){
15.publicbooleanaccept(Filedir,Stringname){
16.return!
name.startsWith(".");
17.}
18.};
19.children=dir.list(filter);
20.
21.//ThelistoffilescanalsoberetrievedasFileobjects
22.File[]files=dir.listFiles();
23.
24.//Thisfilteronlyreturnsdirectories
25.FileFilterfileFilter=newFileFilter(){
26.publicbooleanaccept(Filefile){
27.returnfile.isDirectory();
28.}
29.};
30.files=dir.listFiles(fileFilter);
复制代码
15.在Java中创建ZIP和JAR文件
1.importjava.util.zip.*;
2.importjava.io.*;
3.
4.publicclassZipIt{
5.publicstaticvoidmain(Stringargs[])throwsIOException{
6.if(args.length<2){
7.System.err.println("usage:
javaZipItZip.zipfile1file2file3");
8.System.exit(-1);
9.}
10.FilezipFile=newFile(args[0]);
11.if(zipFile.exists()){
12.System.err.println("Zipfilealreadyexists,pleasetryanother");
13.System.exit(-2);
14.}
15.FileOutputStreamfos=newFileOutputStream(zipFile);
16.ZipOutputStreamzos=newZipOutputStream(fos);
17.intbytesRead;
18.byte[]buffer=newbyte[1024];
19.CRC32crc=newCRC32();
20.for(inti=1,n=args.length;i21.Stringname=args;
22.