Todayfreeman

          BlogJava 聯(lián)系 聚合 管理
            4 Posts :: 2 Stories :: 3 Comments :: 0 Trackbacks

          這個(gè)問題讓我郁悶了兩三天時(shí)間,最后才發(fā)現(xiàn)問題是在JSP端  <html:form action="/uploadsAction"  enctype="multipart/form-data"  >
          標(biāo)簽如果少了"  enctype="multipart/form-data"  服務(wù)器就會(huì)報(bào)錯(cuò),
          下面把代碼貼出來.

          JSP端
           <html:errors />
                    <html:form action="/uploadsAction"  enctype="multipart/form-data"  >
                      <html:file property="theFile" /> 
                       <html:radio property="upType" value="a" />CSVFileReader
                       <html:radio property="upType" value="b" />FileUp      
                      <html:submit value="OK"  />
                    </html:form>
          FormBean中將屬性定義為FormFile,geter seter 方法依舊.
          Action 中的代碼如下:實(shí)現(xiàn)將圖片上傳至UPLOAD文件夾內(nèi) 如果文件大于20K或是寬&高超過規(guī)定范圍的,會(huì)重新勾畫.實(shí)現(xiàn)對上傳圖片的控制.
          當(dāng)然這只是測試Action沒有跳轉(zhuǎn)頁面..

          package upload;

          import org.apache.struts.action.ActionMapping;
          import org.apache.struts.action.ActionForm;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          import org.apache.struts.action.ActionForward;
          import org.apache.struts.action.Action;
          import org.apache.struts.upload.*;
          import java.io.IOException;
          import java.awt.Image;
          import java.awt.image.BufferedImage;
          import com.sun.image.codec.jpeg.JPEGImageEncoder;
          import com.sun.image.codec.jpeg.JPEGCodec;

          import java.io.File;
          import java.io.BufferedInputStream;
          import java.io.BufferedOutputStream;
          import java.io.FileOutputStream;

          public class UploadAction extends Action {
              public ActionForward execute(ActionMapping actionMapping,
                                           ActionForm actionForm,
                                           HttpServletRequest servletRequest,
                                           HttpServletResponse servletResponse)throws Exception {
                  System.out.println("asdasdasdasdasdasd");
                  UploadForm uploadForm = (UploadForm) actionForm;
                  FormFile pic =  uploadForm.getPic();
                  String picname = pic.getFileName();
                  String uploadFileName = servletRequest.getSession()
                                          .getServletContext()
                                          .getRealPath("upload")+"\\"+picname;
                  File upliadFile = new File(uploadFileName);
                  BufferedInputStream bis = null;
                  Image image = null;
                  BufferedOutputStream bos = null;
                  try{
                  if(pic.getFileSize()<2*1024*1024){
                  bis = new BufferedInputStream(pic.getInputStream());
                  image = javax.imageio.ImageIO.read(bis);
                  int width = image.getWidth(null);
                  int height = image.getHeight(null);
                  int w = 160;
                  int h = 120;
                  if(width>w||height>h){
                  BufferedImage bi = new BufferedImage(w,h,
                                                       BufferedImage.TYPE_INT_RGB);
                  bi.getGraphics().drawImage(image,0,0,w,h,null);
                  bos = new BufferedOutputStream(new FileOutputStream(
                 upliadFile));
                  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
               encoder.encode(bi);
                  System.out.println(width * height);
                  }else{
                  bos = new BufferedOutputStream(new FileOutputStream(upliadFile));
                  byte[] date = new byte[5*1024];
                  int len = bis.read(date);
                  while (len!=-1){
                      bos.write(date);
                      len = bis.read(date);
                  }
                  }
                  }
                       return actionMapping.findForward("ok");
                  }catch(Exception e){
                  e.printStackTrace();
              } finally {
                  try {
                      if (bis != null)
                          bis.close();
                  } catch (IOException e1) {

                      e1.printStackTrace();
                  }
                  try {
                      if (bos != null)
                          bos.close();
                  } catch (IOException e2) {

                      e2.printStackTrace();
                  }
              }

                  return actionMapping.findForward("ok");
              }
          }

          posted on 2006-03-13 19:33 當(dāng)理想插上程序的翅膀 閱讀(541) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 满洲里市| 六安市| 惠来县| 武宣县| 津市市| 安义县| 手游| 安新县| 饶阳县| 贡嘎县| 勐海县| 竹溪县| 林芝县| 南通市| 裕民县| 吐鲁番市| 乌海市| 台南市| 松溪县| 怀柔区| 漯河市| 普定县| 惠安县| 延津县| 文昌市| 吉隆县| 加查县| 崇仁县| 米林县| 台南县| 囊谦县| 娱乐| 建湖县| 巴彦淖尔市| 广昌县| 商洛市| 通江县| 昆山市| 新龙县| 焉耆| 西青区|