龍行天下

            政 博
          隨筆 - 23, 文章 - 0, 評論 - 12, 引用 - 0
          數據加載中……

          在Struts框架內實現圖片上傳 (轉載:http://www.aygfsteel.com/Todayfreeman/)

          這個問題讓我郁悶了兩三天時間,最后才發現問題是在JSP端? <html:form action="/uploadsAction"? enctype="multipart/form-data"? >
          標簽如果少了"? enctype="multipart/form-data" ?服務器就會報錯,
          下面把代碼貼出來.

          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 中的代碼如下:實現將圖片上傳至UPLOAD文件夾內 如果文件大于20K或是寬&高超過規定范圍的,會重新勾畫.實現對上傳圖片的控制.
          當然這只是測試Action沒有跳轉頁面..

          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-05-03 20:09 feingto 閱讀(969) 評論(0)  編輯  收藏 所屬分類: Java Web

          主站蜘蛛池模板: 青岛市| 无为县| 五指山市| 北碚区| 昭通市| 湘乡市| 凉城县| 钟山县| 大连市| 通城县| 疏勒县| 宁安市| 敦化市| 巩义市| 新宁县| 陆川县| 武穴市| 许昌市| 宁德市| 北宁市| 大兴区| 文安县| 西盟| 宝鸡市| 故城县| 威远县| 利辛县| 景东| 弋阳县| 长治市| 伊川县| 阿拉善左旗| 库尔勒市| 化州市| 本溪| 正宁县| 黑河市| 奉贤区| 句容市| 井冈山市| 沅陵县|