jsp实现文件上传(一)用jspSmartUpload组件实现文件上传

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);
}

 

转载于:https://www.cnblogs.com/Dreamlu/p/4151256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值