jsp 上傳圖片並生成縮位圖或者加水印--zt
有些網站 動網, 上傳圖片後加給加上自己的字(是在圖片上加的)
請問在JSP裏如何實現??
//添加水印,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.creatGraphics( );
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);
%>
隨機圖片名稱
<%
mySmartUpload.initialize(pageContext);
mySmartUpload.service(request,response);
mySmartUpload.upload();
String fn=mySmartUpload.getFiles().getFile(0).getFileName();
mySmartUpload.save("upload/"); //文件保存的目錄爲upload
out.println("已經成功上傳了文件,請查看<a href=upload/"+fn+">這裏</a>");
%>
上面的程序可以上傳圖片,不過只能上傳gif或者JPG圖片。
而且保存圖片在upload文件夾下面,要想GIF或Jpg圖片的名稱變爲年+月+日+隨機數.gif或年+月+日+隨機數.jpg
只允許上傳jpg或gif圖片,在客戶端用javaScript控制要好些。
變圖片名稱可用如下代碼:自己看看就明白了。:
//得到實際路徑
String realPath = this.masRequest.getRequest().getRealPath("/");
String userPhotoPath = realPath + "images//UserPhoto//";
userPhotoPath = MasString.replace(userPhotoPath,"//","////");
if (!file.getFileName().trim().equals(""))
{
//根據系統時間生成文件名
Date nowTime = new Date();
emp_Photo = userPhotoPath + String.valueOf(nowTime.getTime()) +"."+ file.getFileExt();
file.saveAs(emp_Photo);
System.out.println("file.saveAs() = " + "OK!!!");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.