图片上传并生成缩略图

图片上传并生成缩略图
1 .负责处理图处的bean:
// ---------------------------------------------------------------
package bean;

import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;

public class convertImage {


private String fileInput ;
private String fileOutput ;

public convertImage()
{

}
public String getFileInput() {
return  fileInput;
}
public 
void  setFileInput(String fileInput) {
this .fileInput  =  fileInput;
}
public String getFileOutput() {
return  fileOutput;
}
public 
void  setFileOutput(String fileOutput) {
this .fileOutput  =  fileOutput;
}

public 
void  convert()
{
try  {
File fi 
=   new  File(fileInput);  // 大图文件
File fo  =   new  File(fileOutput);  // 将要转换出的小图文件
int  nw  =   150 // 定义宽为150
int  nh  =   100 // 定义高为100
AffineTransform transform  =   new  AffineTransform();
BufferedImage bis 
=  ImageIO.read(fi);
int  w  =  bis.getWidth();
int  h  =  bis.getHeight();

double  sx  =  ( double )nw / w;
double  sy  =  ( double )nh / h ;
// 判断是横向图形还是坚向图形
if  ( w  >  h )  // 横向图形
{
if  ( ( int )(sx  *  h )  >  nh )  // 比较高不符合高度要求,就按高度比例
{

 

sx 
=  sy ;
nw 
=  ( int )(w * sx) ;
}
else
{
sy 
=  sx ;
nh 
=  ( int )( h * sy) ;
}
}
else
{
if  ( ( int )(sy  *  w )  >  nw ) 
{
sy 
=  sx ;
nh 
=  ( int )(h  *  sy ) ;
}
else
{
sx 
=  sy ;
nw 
=  ( int )(w * sx) ;
}
}

transform.setToScale(sx,sy);
AffineTransformOp ato 
=   new  AffineTransformOp(transform, null );
BufferedImage bid 
=   new  BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid,
" jpeg " ,fo);

catch (Exception e) 
{
e.printStackTrace();
}

}

2 .上传文件的upload.jsp
--------------------------------------------------------------------
<% @ taglib uri = " /WEB-INF/struts-html.tld "  prefix = " html "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >

< title > My JSP 'upload.jsp' starting page </ title >

< meta http - equiv = " pragma "  content = " no-cache " >
< meta http - equiv = " cache-control "  content = " no-cache " >
< meta http - equiv = " expires "  content = " 0 " >
< meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
< meta http - equiv = " description "  content = " This is my page " >

<!--
< link rel = " stylesheet "  type = " text/css "  href = " styles.css " >
-->
</ head >

< body >
< html:form action = " /upload.do "  enctype = " multipart/form-data " >
< html:file property = " theFileone " />
< html:submit />
</ html:form >

</ body >
</ html >
// ------------------------------------------------------------------------
3 .显示成功页面

// ------------------------------------------------------------------------
<% @ page language = " java "  import = " java.util.* "  pageEncoding = " UTF-8 " %>
<% @ page contentType = " text/html;charset=GB2312 "   %>
<%
String path 
=  request.getContextPath();
String basePath 
=  request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
%>

<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
< base href = " <%=basePath%> " >

< title > My JSP 'display.jsp' starting page </ title >

< meta http - equiv = " pragma "  content = " no-cache " >
< meta http - equiv = " cache-control "  content = " no-cache " >
< meta http - equiv = " expires "  content = " 0 " >
< meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
< meta http - equiv = " description "  content = " This is my page " >

<!--
< link rel = " stylesheet "  type = " text/css "  href = " styles.css " >
-->
</ head >

< body >
上传成功. 
< br >
</ body >
</ html >
// ---------------------------------------------------------------------
4 .Action 
// Created by MyEclipse Struts
//
 XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

package com.mk.struts.action;

import java.io.
* ;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.mk.struts.form.UploadForm;

import bean.convertImage;

/*
* MyEclipse Struts
* Creation date: 03-28-2006

* XDoclet definition:
* @struts.action validate="true"
*/
public class UploadAction extends Action {

//  --------------------------------------------------------- Instance Variables

//  --------------------------------------------------------- Methods

/*
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {

//  TODO Auto-generated method stub
String encoding  =  request.getCharacterEncoding() ;
if  ( (encoding  !=   null  ) &&  (encoding.equalsIgnoreCase( " uft-8 " )))
{
response.setContentType(
" text/html;charset=gb2312 " ) ;  // 如果没有找定编码,编码格式设为gb2312 
}

UploadForm theForm 
=  (UploadForm) form ;
FormFile fileone 
=  theForm.getTheFileone() ;  // 取得上传的文件名

try
{
// 开始上传文件
String filePath  =   this .getServlet().getServletContext().getRealPath( " / " ) ;  // 取得当前路径
InputStream stream  =  fileone.getInputStream() ;  // 把文件读入
ByteArrayOutputStream baos  =   new  ByteArrayOutputStream() ;

/*
* 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
*/
OutputStream bos 
=   new  FileOutputStream(filePath  +  fileone.getFileName()) ;

request.setAttribute(
" fileName " ,filePath  +   " / "   +  fileone.getFileName() ) ;
int  bytesRead  =   0  ;
byte [] buffer  =   new   byte [ 8192 ] ;
while ( (bytesRead  =  stream.read(buffer, 0 , 8192 ) )  !=   - 1  )
{
bos.write(buffer,
0 ,bytesRead) ;
}

bos.close();
stream.close() ; 

// 上传文件完成
String oldurl =  filePath  +  fileone.getFileName() ;
String newurl
=  filePath  +   " min_ "   +  fileone.getFileName() ;  // 新的缩略图保存地址

convertImage convert 
=   new  convertImage() ;
convert.setFileInput(oldurl) ;
convert.setFileOutput(newurl) ;
convert.convert() ;

}

catch (Exception e)
{
System.err.print(e) ;
}
return  mapping.findForward( " display " );
}

}
来源:http://www.ideagrace.com/html/doc/2006/04/21/00776.html

你可能感兴趣的:(图片上传并生成缩略图)