---
title: "jsp上传图片并生成缩位图或者加水印 "
author: "daniel_tu"
date: "2009-02-25T16:53:55Z"
tags: ["JSP", "Servlet"]
slug: "jsp"
draft: false
lastmod: "2009-02-25T16:53:55Z"
id: 338649
url: "https://daniel-tu.iteye.com/blog/338649"
---

//添加水印,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 &lt; 0 || i &gt;= s.length() - 1)  
{  
i = s.lastIndexOf("/");  
if(i &lt; 0 || i &gt;= 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 &gt;= 0)  
{  
s2 = s2.substring(i + 10);  
if((i = s2.indexOf("\\"")) &gt; 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") &gt;= 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] &gt; newline.length() * byte0)  
fileoutputstream.write(abyte1, 0, ai1\[0] - newline.length() * byte0);  
if(s3 != null && abyte0\[0] != 45 && ai\[0] &gt; 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页：

&lt;[%@page](mailto:%@page) contentType="text/html;charset=gb2312" import="package.upload"%&gt;  
&lt;%  
String Dir = "c:\\dir\\upload";  
String fn="";  
upload upload = new upload();  
upload.setUploadDirectory(Dir);  
fn=upload.uploadFile(request);  
%&gt;