jsp實現檔案上傳(一)用jspSmartUpload組件實現檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   os   使用   sp   for   

java類(ImageUtil.java)

import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import javax.imageio.ImageIO;public class ImageUtil {    private BufferedImage i = null;/* * 上傳圖片 * */    public void setImg(String imgpath) {        try {            this.i = ImageIO.read(new FileInputStream(imgpath));        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    public ImageUtil() {    }    public ImageUtil(String imgpath) {        try {            this.i = ImageIO.read(new FileInputStream(imgpath));        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }/*  * 圖片加浮水印文字 * */    public void txt(String s, int fontsize, String imgpath) {        Graphics g = this.i.getGraphics();        g.setFont(new Font("隸書", Font.BOLD, fontsize));        // g.setColor(Color.RED);        g.setColor(new Color(255, 255, 255, 80));        g.drawString(s, i.getWidth() - 200, this.i.getHeight() - fontsize - 10);        try {            ImageIO.write(this.i, "jpg", new File(imgpath));        } catch (IOException e) {            e.printStackTrace();        }    }    /*      * 圖片加浮水印logo     * */    public void logo(String logopath, String imgpath) {        Graphics g = this.i.getGraphics();        try {            BufferedImage logo = ImageIO.read(new File(logopath));            g.drawImage(logo, this.i.getWidth() - 189 - 10,                    this.i.getHeight() - 69 - 10, this.i.getWidth() - 10,                    this.i.getHeight() - 10, 0, 0, logo.getWidth(),                    logo.getHeight(), null);            ImageIO.write(this.i, "jpg", new File(imgpath));        } catch (IOException e) {            e.printStackTrace();        }    }
/*      * 縮圖     * */
public void thumd(int ww, int hh, String imgpath) { int w = this.i.getWidth(); int h = this.i.getHeight(); BufferedImage nimg = new BufferedImage(ww, hh, BufferedImage.TYPE_INT_RGB); Graphics g = nimg.getGraphics(); g.drawImage(this.i, 0, 0, ww, hh, 0, 0, w, h, null); try { ImageIO.write(nimg, "jpg", new File(imgpath)); } catch (IOException e) { e.printStackTrace(); } } }

使用上傳照片在jsp中使用

SmartUpload su = new SmartUpload();su.initialize(pageContext);su.upload();ImageUtil iu = new ImageUtil();for(int i=0;i<su.getFiles().getCount();i++){    File f = su.getFiles().getFile(i);    if(f.isMissing()){        continue;    }    UUID u = UUID.randomUUID();    String path = "/upload/"+u.toString()+"."+f.getFileExt();    f.saveAs(path);    String pp = request.getServletContext().getRealPath(path);    iu.setImg(pp);}

 

jsp實現檔案上傳(一)用jspSmartUpload組件實現檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.