加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swf上传

发布时间:2020-12-15 17:27:57 所属栏目:百科 来源:网络整理
导读:?????? 工程目录如图所示,其中css、files、js、swfupload都是它所依赖的文件。用example.jsp来触发action。其中 flash要用swfupload_fp9.swf(之前一直用swfupload.swf,这是比较旧的版本所以触发action后只能发送一张,换成新的版本后可触发多次) example


??????

工程目录如图所示,其中css、files、js、swfupload都是它所依赖的文件。用example.jsp来触发action。其中

flash要用swfupload_fp9.swf(之前一直用swfupload.swf,这是比较旧的版本所以触发action后只能发送一张,换成新的版本后可触发多次)

example.jsp代码如下:

[html]? view plain copy
  1. <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">??
  2. <html>??
  3. ??head ????meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8" ????title>SWFUpload简单使用例子?Java版(JSP)</ ??????
  4. meta?http-equiv="pragma"?content="no-cache"meta?http-equiv="cache-control"?content="no-cache"meta?http-equiv="expires"?content="0">??????
  5. meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3"meta?http-equiv="description"?content="This?is?my?page"script?type="text/javascript"?src="swfupload/swfupload.js">scriptscript?type="text/javascript"?src="js/swfupload.queue.js"script?type="text/javascript"?src="js/fileprogress.js"script?type="text/javascript"?src="js/handlers.js" ??????
  6. link?href="css/default.css"?rel="stylesheet"?type="text/css"?/>??
  7. ??script?type="text/javascript" ??var?upload;??
  8. ??window.onload?=?function()?{??
  9. ????upload?=?new?SWFUpload({??
  10. ????//?处理文件上传的url??
  11. ????upload_url:?"http://localhost:8080/resirectJSP/zoom.action?upload=1",???//?路径写全,否则Firefox下会出现404错误。自由修改处一:处理文件上传的url路径,注意还要写全部??
  12. ??
  13. ????//?上传文件限制设置??
  14. ????file_size_limit?:?"10240",??//?10MB??
  15. ????file_types?:?"*.jpg;*.gif;*.png",???//此处也可以修改成你想限制的类型,比如:*.doc;*.wpd;*.pdf??
  16. ????file_types_description?:?"Image?Files",??
  17. ????file_upload_limit?:?"0",??
  18. ????file_queue_limit?:?"50",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ??
  19. ????//?事件处理设置(所有的自定义处理方法都在handler.js文件里)??
  20. ????file_dialog_start_handler?:?fileDialogStart,248)"> ????file_queued_handler?:?fileQueued,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????file_queue_error_handler?:?fileQueueError,248)"> ????file_dialog_complete_handler?:?fileDialogComplete,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????upload_start_handler?:?uploadStart,248)"> ????upload_progress_handler?:?uploadProgress,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????upload_error_handler?:?uploadError,248)"> ????upload_success_handler?:?uploadSuccess,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????upload_complete_handler?:?uploadComplete,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????//?按钮设置??
  21. ????button_image_url?:?"swfupload/xpbutton.png",????//?按钮图标??
  22. ????button_placeholder_id?:?"spanButtonPlaceholder",248)"> ????button_width:?61,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????button_height:?22,248)"> ??????????????????
  23. ????//?swf设置??
  24. ????flash_url?:?"swfupload/swfupload_fp9.swf",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ??????????????????
  25. ????custom_settings?:?{??
  26. ????????progressTarget?:?"fsUploadProgress",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????????cancelButtonId?:?"btnCancel"??
  27. ????},248)"> ????????//?Debug?设置??
  28. ????????debug:?false??
  29. ????});??
  30. ??};??
  31. ???bodydiv?class="flash"?id="fsUploadProgress"divdiv?style="padding-left:?5px;" ????????span?id="spanButtonPlaceholder"span ????????input?id="btnCancel"?type="button"?value="取消"?onClick="cancelQueue(upload);"?disabled="disabled"?style="margin-left:?2px;?height:?22px;?font-size:?8pt;"? >??

action代码如下:


[java]? copy
    package?swf;??
  1. import?java.io.File;??
  2. import?java.io.IOException;??
  3. import?java.text.SimpleDateFormat;??
  4. import?java.util.Date;??
  5. import?javax.servlet.http.HttpServletRequest;??
  6. import?javax.servlet.http.HttpServletResponse;??
  7. import?org.apache.struts2.ServletActionContext;??
  8. import?org.apache.struts2.interceptor.ServletResponseAware;??
  9. import?com.opensymphony.xwork2.ActionContext;??
  10. import?com.opensymphony.xwork2.ActionSupport;??
  11. public?class?UploadPicture?extends?ActionSupport?implements?ServletResponseAware{??
  12. ????/**?
  13. ?????*??
  14. ?????*/??
  15. ????private?static?final?long?serialVersionUID?=?7566364916209313775L;??
  16. ????private?File?Filedata;??
  17. private?String?imgFileContentType;??
  18. int?i?=0;??
  19. ????HttpServletResponse?response;??
  20. public?String?getImgFileContentType()?{??
  21. ????????return?imgFileContentType;??
  22. ????}??
  23. void?setImgFileContentType(String?imgFileContentType)?{??
  24. this.imgFileContentType?=?imgFileContentType;??
  25. public?File?getFiledata()?{??
  26. return?Filedata;??
  27. void?setFiledata(File?filedata)?{??
  28. ????????Filedata?=?filedata;??
  29. /************************************************************************/??
  30. void?uploadPicture()??
  31. ????{?????
  32. ????????HttpServletRequest?request?=?ServletActionContext.getRequest();??
  33. ????????System.out.println(request.getParameter("upload"));??
  34. ????????System.out.println(Filedata);??
  35. ????????System.out.println(Filedata.getName());??
  36. ????????System.out.println("load");??
  37. ????????String?path?=?"E:Tomcat?6.0webappsresirectJSPfiles";??
  38. ????????SimpleDateFormat?sdf=new?SimpleDateFormat("yyyyMMddkkmmss");//以当前精确到秒的日期为上传的文件的文件名??
  39. ????????String?imgFileName?=?sdf.format(new?Date())+?i++?+".jpg";??
  40. ??????????
  41. ????????PictureZoom?pz?=?new?PictureZoom(Filedata,path,imgFileName);??
  42. ????????if(pz.CreateThumbnail(700).equals("error"))??
  43. ????????{??
  44. ????????????System.out.println("照片上传失败");??
  45. ????????}else{??
  46. ????????????System.out.println("上传成功");??
  47. ????????????try?{??
  48. ????????????????response.getWriter().write("uploadsuccess:"+imgFileName);??
  49. ????????????}?catch?(IOException?e)?{??
  50. ????????????????//?TODO?Auto-generated?catch?block??
  51. ????????????????e.printStackTrace();??
  52. ????????????}??
  53. ????????}??
  54. ????}??
  55. void?setServletResponse(HttpServletResponse?response)?{??
  56. ????????//?TODO?Auto-generated?method?stub??
  57. this.response?=?response;??
  58. }??

官方版本的是用php或是jsp接收的,在把jsp改成action时特别痛苦,后来经过各种渠道得到了flash传图片时的文件域的name="Filedata".结果用struts2时就在action里命名相同的File Filedata;

PictureZoom.java代码如下:

copy
    import?java.io.ByteArrayInputStream;??
  1. import?java.io.File;??
  2. import?java.io.IOException;??
  3. import?java.io.InputStream;??
  4. import?java.awt.image.BufferedImage;??
  5. import?java.awt.Image;??
  6. import?java.awt.image.AffineTransformOp;??
  7. import?javax.imageio.ImageIO;??
  8. import?javax.imageio.stream.ImageInputStream;??
  9. import?java.awt.geom.AffineTransform;??
  10. class?PictureZoom?{??
  11. private?String?todir;?//?todir?处理后的图片存放目录??
  12. private?String?sysimgfile;?//?sysimgfile?处理后的图片文件名前缀??
  13. private?File?F;??
  14. public?PictureZoom(){}??
  15. public?PictureZoom(File?file,String?todirNow,String?sysimgfileNow)?{??
  16. this.todir?=?todirNow;??
  17. ????????????this.sysimgfile?=?sysimgfileNow;??
  18. this.F?=?file;??
  19. ????????}??
  20. public?String?createThumbnail(int?width,int?height,153); font-weight:bold; background-color:inherit">byte[]?b,File?file)??
  21. ????{??
  22. double?widthRatio?=?1.0;??
  23. double?heightRatio?=?1.0;??
  24. ??????????
  25. ????????InputStream?is?=?new?ByteArrayInputStream(b);??
  26. ????????BufferedImage?Bi?=?null;??
  27. try?{??
  28. ????????????Bi?=?ImageIO.read(is);??
  29. ????????}?catch?(IOException?e)?{??
  30. ????????????//?TODO?Auto-generated?catch?block??
  31. ????????????e.printStackTrace();??
  32. ????????????System.out.println("byte[]转成InputStream出错");??
  33. return?"error";??
  34. ?????????Image?Itemp?=?Bi.getScaledInstance(width,?height,?Bi.SCALE_FAST);??
  35. if(Bi.getHeight()>height)??
  36. ????????????????heightRatio?=?Float.valueOf(height)?/?Bi.getHeight();??
  37. if(Bi.getWidth()>width)??
  38. ????????????????widthRatio?=?Float.valueOf(width)?/?Bi.getWidth();??
  39. ??????????????
  40. ????????????System.out.println(Bi.getHeight());??
  41. ????????????System.out.println(Bi.getWidth());??
  42. ????????????AffineTransformOp?op?=?new?AffineTransformOp(AffineTransform??
  43. ????????????????????.getScaleInstance(heightRatio,?widthRatio),?null);??
  44. ????????????Itemp?=?op.filter(Bi,153); font-weight:bold; background-color:inherit">null);??
  45. ??????????????
  46. ????????????
  47. ????????????????ImageIO.write((BufferedImage)?Itemp,?"jpg",?file);??
  48. ????????????}? ???????????????? ????????????????e.printStackTrace();??
  49. ????????????????return?"error";??
  50. return?"success";??
  51. public?String?CreateThumbnail(int?height){??
  52. //?fileExtNmae是图片的格式?gif?jpg?或png??
  53. ????????????//?String?fileExtNmae="";??
  54. if?(!F.isFile())??
  55. ????????????{??
  56. ????????????File?ThF?=?new?File(todir,?sysimgfile);??
  57. ????????????BufferedImage?Bi;??
  58. ????????????????Bi?=?ImageIO.read(F);??
  59. catch?(IOException?e1)?{??
  60. ????????????????e1.printStackTrace();??
  61. ????????????
  62. ????????????Image?Itemp?=?Bi.getScaledInstance(width,?ThF);??
  63. return?"success";??
  64. ???????
  65. ???????
  66. ?????int?width){??
  67. //?fileExtNmae是图片的格式?gif?jpg?或png??
  68. //?String?fileExtNmae="";??
  69. ????????????????????.getScaleInstance(widthRatio,248)"> }??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读