The example of this article describes the method of uploading pictures to the website. Share it for everyone for your reference. The specifics are as follows:
Recently, after a while, I went to the alumni record. The pictures of the alumni records will be added with a chapter. Hehe, I did one, but it was only suitable for JPG format. Send it for everyone to study. Welcome to discuss!
Very old code
/*************************************************** <p> Java's operation of the picture (only JPG) </p>* signing of the picture <br>* The shrinkage of the picture <br>* <p> Title: Java's operation of the picture (only can only be only possible Use JPG) </p>* <p> Createdata: 2004-12-2 </p>* <p> Description: </p>* <p> CopyRight: Copyright (C) 2004 </p>* @AUTHORORRRRR王凯* @version 1.0******************************************** ***/package com.cn.wangk.test; Import java.io.*; Import com.sun.image.codec.jpeg.*; // Sun only provided the coding of jpg picture files anpiimport java.awt. Font; Import Java.awt.graphics; Import Java.awt.image; Import Java.awt.Geom.affinetransform; Import Java.awt.Image.AffinetransFormop; Import ava.awt.image.bufferedImage; Import java.awt.image. ImageObserver;import javax.imageio.ImageIO;/*** @author wangkai*/public class Test { /** * */ public Test() { try { //生成以后新的图片地址File fo = new File(" c: //4.jpg "); // The picture files read imagepath =" c: // documents and settings // administrator " +" // my painnts // my pictures // 1.jpg ";/ /Seal the picture file string toiagepth = "c: //1.jpg"; // The file stream of the picture inputStream Imagein; Imagein = new fileinputStream (new file (ImagePath)); // The input encoder will be obtained. File stream JPEGIMAGEDECODER Decoder = JPEGCODEC.CREATEJPEGDECODER (Imagein); // The encoded image object bufferedImage Image = DECODEASBUFFEREDIMA is obtained. Ge (); Graphics g = Image.getGraphics (); Try {inputStream Imagein2 = Null; Imagein2 = New FileInputStream (New File (toimagePth)); // Get the input encoder, encode the file stream in jpegImageDeCoder Decoder2 = jpegcodecoder (ImageIn2); // The encoded image object bufferedImage Image2 = DECODER2.DecodeasbuffEredImage (); // Cover the picture seal imageobserver observation = null; int x = Image.getWidth () -Image2.getWidth (); int rawimage (image2, x, y, obser);} Catch (FilenotFoundexception E) {// Open the file fails, indicating that the chapter picture does not exist. At this time , 18)); g.Drawstring ("Qiu Shui Studio", Image.getWidth () -100, Image.getheight () -20); g.drawstring ("[email protected]", Image.getWidth ( ) -180, Image.getheight () -10);} g.dispose (); Imageio.write (Image, "JPEG", FO); System.out.println ("OK"); On E) {// automatically generate CATCH block E.printStacktrace ();} Catch (ImageFormatexception E) {// Automatically generate CATCH block E.printstacktrace ();} Catch (IOEXCEPTION) automatically generates CATCH block E. Printstacktrace () ;}} Public Static Void Savefixedboundicon (File ImageFile, Int Height, Int Width) Throws Exception {Double Ratio = 0.0; IF (ImageFile == Null ||! Magefile.isfile ()) Throw New Exception (ImageFile + "cannot be found Specify files! "); String filepath = ImageFile.getpath (); buffredimage bi = Imageio.read (ImageFile); if (bi.getheight ()> deep) || (bi.getWidth ()> width)) {{ if (bi.getHeight ()> bi.getWidth ()) {ratio = (New Integer (Height)). Doublevalue () / bi.getheight ();} Else {ratio = (new integer (width)). Do UbleValue ( ) / Bi.getWidth ();} File Savefile = New File (Filepath + "_" + Height + "_" + Width + ".jpg"); Image itmp = bi.getscaledance (Width, Height, IM Age.scale_smooth) ; Affinetransformop OP = New Affinetransformop (AFFINETRANSFORM.GetScaleInstance (RATIO, RATIO), NULL); Itemp = OP.Filter (BI, NULL); Try { Te (bufferedInage) itemp, "jpeg", savefile);} Catch (Exception EX) {}}} Public Static Void Main (String [] ARGS) {// test ts = new test (); try {test.savefixedboundicon (new file ("c: //test.jpg"), 200 , 200);} Catch (Exception E) {// Automatically generate CATCH block e.printstacktrace ();}}}It is hoped that this article is helpful to everyone's Java program design.