圖片上傳並生成縮略圖

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");
    }


}

//---------------------------------------------------------------------------


//其它的看Myeclipse+struts源文件,源代碼下次傳上,

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章