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组件实现文件上传的更多相关文章

  1. 【java】[文件上传jar包]commons-fileUpload组件解决文件上传(文件名)乱码问题

    response.setContentType("text/html; charset=UTF-8");  Boolean isMultipart = ServletFileUpl ...

  2. 关于GC(上):Apache的POI组件导致线上频繁FullGC问题排查及处理全过程

    某线上应用在进行查询结果导出Excel时,大概率出现持续的FullGC.解决这个问题时,记录了一下整个的流程,也可以作为一般性的FullGC问题排查指导. 1. 生成dump文件 为了定位FullGC ...

  3. vue 父组件给子组件传值 Vue父组件给子组件传方法 Vue父组件把整个实例传给子组件

    Home.vue <template> <!-- 所有的内容要被根节点包含起来 --> <div id="home"> <v-header ...

  4. JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

    JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...

  5. jsp实现文件上传(二)用cos组件实现文件上传

    jsp表单 <%@ page language="java" pageEncoding="utf-8"%> <html> <hea ...

  6. Struts2文件上传(基于表单的文件上传)

    •Commons-FileUpload组件 –Commons是Apache开放源代码组织的一个Java子项目,其中的FileUpload是用来处理HTTP文件上传的子项目   •Commons-Fil ...

  7. ueditor1.4.3配置过程(包含单独上传文件以及图片的使用),ueditor1.4.3上传配置(转 http://www.bkjia.com/webzh/1001016.html)

    这里使用的是ueditor1.4.3的jsp版本的UTF-8版本. 首先下载相应的ueditor,将ueditor文件夹直接拷贝到项目中,文件结构如下所示: 然后将项目要用的jar包导入到lib目录下 ...

  8. Java Web文件上传原理分析(不借助开源fileupload上传jar包)

    Java Web文件上传原理分析(不借助开源fileupload上传jar包) 博客分类: Java Web   最近在面试IBM时,面试官突然问到:如果让你自己实现一个文件上传,你的代码要如何写,不 ...

  9. 使用commons-fileupload-1.2.1.jar等组件实现文件上传

    使用的主要jar包:commons-io-1.3.2.jar包;commons-fileupload-1.2.1.jar包:commons-lang-2.3.jar,在使用组件实现文件上传时候要注意前 ...

随机推荐

  1. Shiro源代码分析之两种Session的方式

    1.Shiro默认的Session处理方式 <!-- 定义 Shiro 主要业务对象 --> <bean id="securityManager" class=& ...

  2. OHIFViewer meteor build 问题

    D:\Viewers-master\OHIFViewer>meteor build --directory d:/h2zViewerC:\Users\h2z\AppData\Local\.met ...

  3. BUPT复试专题—哈夫曼树(2010)

    https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage= ...

  4. C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程

    C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程   串口的定义,请自行了解. C#操作串口通讯在.Net强大类库的支持下,只需要三个步骤: 1 创建 2 打开 3 发送/接受 ...

  5. ubuntu 用shell脚本实现将当前文件夹下全部文件夹中的某一类文件复制到同一文件夹下

    当前文件夹下有一些文件和文件夹,当中每一个文件夹里都有若干.txt文件. 如今要求在当前文件夹创建一个新文件夹all,且将那些文件夹全部.txt文件 都复制到文件夹all.在ubuntu12.04的s ...

  6. innodb 乐观插入因空间不够导致失败,进入悲观插入阶段,这个空间的大小限制

    btr_cur_optimistic_insert{ ... /*检查分裂页时是否有足够的空间预留给未来记录的update*/ if (leaf && !zip_size && ...

  7. stm32GPIO8种模式

    stm32GPIO工作模式及用途  1.浮空输入GPIO_IN_FLOATING ——浮空输入,可以做KEY识别,RX1       2.带上拉输入GPIO_IPU——IO内部上拉电阻输入       ...

  8. PyOpenGL下GlutBitmapCharacter的替代

    虽然pyinstaller支持pyopengl,但是调用GLUT之后,在其它电脑上面运行就会出现错误,索性按照之前C#上面的办法,把字体数据和函数用python重写 fontData.py #! /u ...

  9. 查询历史使用过的命令并使用(history)

    一.什么是history 在bash功能中.它能记忆使用过的命令,这个功能最大的优点就是能够查询以前做过的举动.从而能够知道你的执行步骤.那么就能够追踪你曾下达过的命令.以作为除错的工具. 二.His ...

  10. WebGIS中地图恢复初始位置及状态

    我想实现这么一个效果:地图任意缩放后,点击一个按钮,将立刻回到地图初始加载时的位置,并且是没有缩放的状态. 怎么办呢?最好的办法就是用Home按钮. <!DOCTYPE HTML> < ...