悟心

          成功不是將來才有的,而是從決定去做的那一刻起,持續累積而成。 上人生的旅途罷。前途很遠,也很暗。然而不要怕。不怕的人的面前才有路。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            93 隨筆 :: 1 文章 :: 103 評論 :: 0 Trackbacks
          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
          posted on 2010-08-29 16:11 艾波 閱讀(429) 評論(0)  編輯  收藏 所屬分類: Application
          主站蜘蛛池模板: 三河市| 应城市| 沅陵县| 凤山县| 泽普县| 高要市| 天柱县| 津市市| 兴安盟| 靖安县| 鹰潭市| 江西省| 建瓯市| 郎溪县| 海安县| 万全县| 铁岭县| 定州市| 富裕县| 邮箱| 独山县| 横峰县| 内黄县| 泰兴市| 浪卡子县| 保靖县| 公主岭市| 航空| 余干县| 贵南县| 芦溪县| 思南县| 五家渠市| 红安县| 九寨沟县| 当涂县| 汝南县| 丰都县| 新宁县| 红安县| 武宁县|