`
daniel_tu
  • 浏览: 182632 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jsp上传图片并生成缩位图或者加水印

    博客分类:
  • J2EE
阅读更多

//添加水印,filePath 源图片路径, watermark 水印图片路径
public static boolean createMark(String filePath,String watermark) {
ImageIcon imgIcon=new ImageIcon(filePath);
Image theImg =imgIcon.getImage();
ImageIcon waterIcon=new ImageIcon(watermark);
Image waterImg =waterIcon.getImage();
int width=theImg.getWidth(null);
int height= theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
Graphics2D g=bimage.createGraphics( );
g.setColor(Color.red);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null );
g.drawImage(waterImg, 100, 100, null );
g.drawString("12233",10,10); //添加文字
g.dispose();
try{
FileOutputStream out=new FileOutputStream(filePath);
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(50f, true);
encoder.encode(bimage, param);
out.close();
}catch(Exception e){ return false; }
return true;
}

/////////////////范例////////////////////
package package;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

public class upload
{

private static String newline = "\n";
private String uploadDirectory;
private String ContentType;
private String CharacterEncoding;

public upload()
{
uploadDirectory = ".";
ContentType = "";
CharacterEncoding = "";
}

private String getFileName(String s)
{
int i = s.lastIndexOf("\\");
if(i < 0 || i >= s.length() - 1)
{
i = s.lastIndexOf("/");
if(i < 0 || i >= s.length() - 1)
return s;
}
return s.substring(i + 1);
}

public void setUploadDirectory(String s)
{
uploadDirectory = s;
}

public void setContentType(String s)
{
ContentType = s;
int i;
if((i = ContentType.indexOf("boundary=")) != -1)
{
ContentType = ContentType.substring(i + 9);
ContentType = "--" + ContentType;
}
}

public void setCharacterEncoding(String s)
{
CharacterEncoding = s;
}

public String uploadFile(HttpServletRequest httpservletrequest)
throws ServletException, IOException
{
String s = null;
setCharacterEncoding(httpservletrequest.getCharacterEncoding());
setContentType(httpservletrequest.getContentType());
s = uploadFile(httpservletrequest.getInputStream());
return s;
}

public String uploadFile(ServletInputStream servletinputstream)
throws ServletException, IOException
{
String s = null;
String s1 = null;
byte abyte0[] = new byte[4096];
byte abyte1[] = new byte[4096];
int ai[] = new int[1];
int ai1[] = new int[1];
String s2;
while((s2 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
int i = s2.indexOf("filename=");
if(i >= 0)
{
s2 = s2.substring(i + 10);
if((i = s2.indexOf("\"")) > 0)
s2 = s2.substring(0, i);
break;
}
}
s1 = s2;
if(s1 != null && !s1.equals("\""))
{
s1 = getFileName(s1);
String s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding);
if(s3.indexOf("Content-Type") >= 0)
readLine(abyte0, ai, servletinputstream, CharacterEncoding);
File file = new File(uploadDirectory, s1);
FileOutputStream fileoutputstream = new FileOutputStream(file);
while((s3 = readLine(abyte0, ai, servletinputstream, CharacterEncoding)) != null)
{
if(s3.indexOf(ContentType) == 0 && abyte0[0] == 45)
break;
if(s != null)
{
fileoutputstream.write(abyte1, 0, ai1[0]);
fileoutputstream.flush();
}
s = readLine(abyte1, ai1, servletinputstream, CharacterEncoding);
if(s == null || s.indexOf(ContentType) == 0 && abyte1[0] == 45)
break;
fileoutputstream.write(abyte0, 0, ai[0]);
fileoutputstream.flush();
}
byte byte0;
if(newline.length() == 1)
byte0 = 2;
else
byte0 = 1;
if(s != null && abyte1[0] != 45 && ai1[0] > newline.length() * byte0)
fileoutputstream.write(abyte1, 0, ai1[0] - newline.length() * byte0);
if(s3 != null && abyte0[0] != 45 && ai[0] > newline.length() * byte0)
fileoutputstream.write(abyte0, 0, ai[0] - newline.length() * byte0);
fileoutputstream.close();
}
return s1;
}

private String readLine(byte abyte0[], int ai[], ServletInputStream servletinputstream, String s)
{
ai[0] = servletinputstream.readLine(abyte0, 0, abyte0.length);
if(ai[0] == -1)
return null;
break MISSING_BLOCK_LABEL_27;
Object obj;
obj;
return null;
if(s == null)
return new String(abyte0, 0, ai[0]);
return new String(abyte0, 0, ai[0], s);
obj;
return null;
}

}


JSP页:

<%@page contentType="text/html;charset=gb2312" import="package.upload"%>
<%
String Dir = "c:\dir\upload";
String fn="";
upload upload = new upload();
upload.setUploadDirectory(Dir);
fn=upload.uploadFile(request);
%>

分享到:
评论

相关推荐

    jsp上传图片并生成缩略图

    综上所述,实现"jsp上传图片并生成缩略图"的过程涉及到文件上传处理、图片的读取与缩放、文件存储以及错误处理等多方面技术。通过合理的设计和选用合适的工具库,可以在JSP中有效地实现这一功能。在实际开发中,还...

    JSP上传图片并生成缩略图

    在这个特定的场景中,"JSP上传图片并生成缩略图"是一个常见的功能需求,尤其是在开发包含用户交互和多媒体内容的Web应用时。下面我们将详细探讨这一技术实现的关键知识点。 首先,**上传组件**是Web应用中用于接收...

    jsp是上传图片加水印

    综上所述,实现"jsp是上传图片加水印"的功能,需要理解JSP的文件上传处理,熟悉Servlet API,掌握Java图像处理技术,并能够处理好用户交互和错误控制。通过这样的实践,可以提升开发者在Web开发中的综合能力,特别是...

    jSP上传图片生成缩略图

    在这个特定的场景中,"jSP上传图片生成缩略图"是一个常见的需求,尤其在网站开发中,如论坛、博客或电商网站等,用户可能需要上传图片,而为了提高页面加载速度和优化用户体验,通常会生成缩略图作为预览。...

    基于jsp实现图片网页视频加水印

    【标题】:“基于jsp实现图片网页视频加水印”这一项目是软件工程课程的一个实践作业,旨在通过Java Web技术实现在网页、图片以及视频上添加水印的功能。这个任务不仅涵盖了基本的编程技能,还涉及到了软件开发的全...

    jsp在线编辑器可以批量上传图片/加水印

    【标题】:“jsp在线编辑器可以批量上传图片/加水印” 在网页开发中,有时候我们需要用户能够在线编辑文本,并且能方便地上传图片。JSP(JavaServer Pages)是一种服务器端脚本语言,用于创建动态网页。在这个场景...

    Jsp图片上传资料并将图片路径上传至数据库

    本文将详细讲解如何实现一个基于JSP的图片上传功能,并将图片路径存储到MSSQL2000数据库中的技术要点。 首先,我们需要理解JSP(JavaServer Pages)的角色。JSP是一种动态网页技术,它允许开发者在HTML页面中嵌入...

    jsp 上传图片并保存到数据库里的servlet代码和jsp代码

    本主题将深入探讨如何使用JSP和Servlet来实现一个图片上传功能,并将图片数据存储到Oracle数据库中。 首先,我们需要理解JSP和Servlet的角色。JSP是用于创建动态内容的视图层技术,而Servlet则是Java编写的应用程序...

    jsp图片文字同时上传并生成缩略图

    根据提供的文件信息,我们可以总结出以下关于“jsp图片文字同时上传并生成缩略图”的相关知识点: ### 一、背景介绍 在Web开发过程中,经常需要处理用户上传的数据,包括但不限于图片、文档等多媒体文件。而在某些...

    jsp添加图片水印程序

    Uploadify使得用户可以选择多张图片并一次性上传,同时提供了进度条反馈,提升用户体验。在我们的项目中,Uploadify会负责接收用户选择的图片,并将它们发送到服务器进行处理。 实现这个功能的具体步骤如下: 1. *...

    JSP上传图片

    本话题聚焦于“JSP上传图片”,这是一个常见的功能需求,特别是在用户交互丰富的网站中,如社交网络、博客平台等,用户可能需要上传个人头像或分享图片。 首先,理解JSP上传图片的基本流程: 1. **前端界面**:用户...

    在JSP上传图片前预览图片

    在JSP上传图片前预览图片在JSP上传图片前预览图片

    JSP验证图片码生成

    **JSP验证图片码生成**是Web开发中一种常见的安全机制,主要用于防止自动化脚本或机器人进行非法操作,如防止恶意注册、重复提交等。在这个压缩包中,包含了一个名为`yz.jsp`的文件,这是一个基于JavaServer Pages ...

    jsp上传图片直接显示在页面上,写好的代码

    上传图片时,选择图片之后显示在页面上,现成的代码,直接使用即可

    JSP源码,图片上传,进度条,JSP图片上传实例

    在这个“JSP源码,图片上传,进度条,JSP图片上传实例”的主题中,我们将深入探讨如何使用JSP进行图片上传,并结合进度条展示上传进度。 首先,图片上传功能是网站或应用程序中常见的一种交互需求。用户可以通过这种...

Global site tag (gtag.js) - Google Analytics