jquery的Uploadify控件上传图片和预览使用介绍。

在简单的servlet系统中和在SSH框架中,后台处理不同的,在三大框架中图片预览时费了不少力气,所以下面将两种情况都介绍一下。

1,前台代码

script:

  1. $("#uploadify").uploadify({
  2. 'langFile'       : '<%=request.getContextPath()%>/config/juploadify/uploadifyLang_cn.js',
  3. 'swf'            : '<%=request.getContextPath()%>/config/juploadify/uploadify.swf',
  4. 'uploader'       : '<%=request.getContextPath()%>/UploadAction/uploadImg.action', //图片上传的action方法
  5. 'queueID'        : 'fileQueue',
  6. 'auto'           : true,
  7. 'multi'          : true,
  8. 'buttonCursor'   : 'hand',
  9. 'fileObjName'    : 'uploadify',
  10. 'buttonText'     : '上传图片',
  11. 'height'         : '25',
  12. 'progressData'   : 'percentage',
  13. 'fileTypeDesc'   : '支持格式:jpg/gif/jpeg/png/bmp.',
  14. 'fileTypeExts': '*.jpg;*.gif;*.jpeg;*.png;*.bmp',//允许的格式
  15. onUploadSuccess  : function(file,data,response) {
  16. //设置图片预览
  17. var _arr_val = data.split(",");
  18. $("#photo").val(_arr_val[1]);
  19. $("#pic_view").attr("src","<%=request.getContextPath()%>" + _arr_val[1]);
  20. }
  21. });

html

  1. <td width="15%" rowspan="9" align="center">
  2. <div id="fileQueue" style="width:200px;"></div>
  3. <input type="file" name="uploadify" id="uploadify" />
  4. <input type="hidden" name="photo" id="photo" value="">
  5. <div id="pic_view_div" style="width:150px; height:180px">
  6. <img src="<%=request.getContextPath()%>/image/nopic.gif" width="150" height="180" id="pic_view">
  7. </div>
  8. </td>

2,servlet中后台处理

  1. public void uploadImg(){
  2. // 解决服务器端消息返回客户端后中文字符乱码
  3. response.setHeader("Content-Type", "text/html;charset=UTF-8");
  4. HttpSession session = request.getSession();
  5. PrintWriter outs = response.getWriter();
  6. //获取上传图片的路径
  7. String savePath = request.getSession().getServletContext().getRealPath("");
  8. String saveDirectory = savePath + "/Imgupload";
  9. File file = new File(saveDirectory);
  10. if (!file.exists()) {
  11. file.mkdirs();
  12. }
  13. //设置图片大小
  14. if (StringUtils.isBlank(this.fileSizeLimit.toString())) {
  15. this.fileSizeLimit = "100";// 默认100M
  16. }
  17. int maxPostSize = Integer.valueOf(this.fileSizeLimit).intValue() * 1024 * 1024;
  18. String encoding = "UTF-8";
  19. MultipartRequest multi = null;
  20. try {
  21. multi = new MultipartRequest(request, saveDirectory, maxPostSize, encoding);
  22. } catch (Exception e) {
  23. System.out.println("发生异常:" + e.getMessage());
  24. return;
  25. }
  26. //图片预览功能实现
  27. String _result = "";
  28. Enumeration files = multi.getFileNames();
  29. while (files.hasMoreElements()) {
  30. String name = (String) files.nextElement();
  31. File f = multi.getFile(name);
  32. if (f != null) {
  33. String fileName = multi.getFilesystemName(name);
  34. String lastFileName = saveDirectory + "/" + fileName;
  35. result += multi.getOriginalFileName(name) + "," + savePath + "/" + fileName;
  36. }
  37. }
  38. outs.print(_result);
  39. }

注意:action方法返回的字符串为图片的名称和图片的路径。

3,SSH框架中使用

  1. package cn.com.zzcy.base.action;
  2. import java.io.File;
  3. @Namespace("/UploadAction")
  4. public class UploadAction {
  5. private File uploadify;
  6. public File getUploadify() {
  7. return uploadify;
  8. }
  9. public void setUploadify(File uploadify) {
  10. this.uploadify = uploadify;
  11. }
  12. private String uploadifyFileName;
  13. public String getUploadifyFileName() {
  14. return uploadifyFileName;
  15. }
  16. public void setUploadifyFileName(String uploadifyFileName) {
  17. this.uploadifyFileName = uploadifyFileName;
  18. }
  19. /**
  20. * 上传图片
  21. * @throws IOException
  22. */
  23. @Action("uploadImg")
  24. public void uploadImg(){
  25. HttpServletRequest request = ServletActionContext.getRequest();
  26. HttpServletResponse response = ServletActionContext.getResponse();
  27. String savePath = request.getSession().getServletContext().getRealPath("");
  28. PrintWriter out = null;
  29. String resultStr = "";
  30. String extName = "";// 扩展名
  31. String newFileName = "";// 新文件名
  32. try {
  33. response.setCharacterEncoding("utf-8");
  34. out = response.getWriter();
  35. //获取文件的扩展名
  36. if (uploadifyFileName.lastIndexOf(".") >= 0) {
  37. extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."));
  38. }
  39. //根据当前时间生成新的文件名称
  40. String nowTime = new SimpleDateFormat("yyyymmddHHmmss").format(new Date());// 当前时间
  41. newFileName = nowTime + extName;
  42. //设置文件保存路径
  43. String param = request.getParameter("param");
  44. savePath = savePath + "/ImgUpload/"+param+"/";
  45. File file = new File(savePath);
  46. if(!file.exists()){
  47. file.mkdirs();
  48. }
  49. uploadify.renameTo(new File(savePath+newFileName));
  50. resultStr = uploadifyFileName+","+"/ImgUpload/"+param+"/"+newFileName;
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }finally{
  54. out.print(resultStr);
  55. }
  56. }
  57. }

注意:要定义局部变量并提供set方法,这样图片的名称和信息才能自动注入进入。

这里通过request方法获取不到图片信息。。。

4,实现的效果图如下:

原文出自:http://blog.csdn.net/lishuangzhe7047/article/details/41803175

Uploadify 控件上传图片 + 预览的更多相关文章

  1. jquery实现上传图片预览(需要浏览器支持html5)

    jquery实现上传图片预览(需要浏览器支持html5) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  2. HTML5上传图片预览

    <!DOCTYPE html> <html> <head> <title>HTML5上传图片预览</title> <meta http ...

  3. HTML5上传图片预览功能

    HTML5上传图片预览功能 HTML代码如下: <!-- date: 2018-04-27 14:41:35 author: 王召波 descride: HTML5上传图片预览功能 --> ...

  4. JS 上传图片 + 预览功能(一)

    JS 上传图片 + 预览功能 <body> <input type="file" id="fileimg1" style="disp ...

  5. js兼容火狐显示上传图片预览效果

    js兼容火狐显示上传图片预览效果[谷歌也适用] <!doctype html> <html> <head> <meta content="text/ ...

  6. 通过HTML5 FileReader实现上传图片预览功能

    原文:http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201706224590.html 在上传图片到服务器之前,我们希望可以预览一下要上传的图片. ...

  7. 去除ckeditor上传图片预览中的英文字母

    去除ckeditor上传图片预览中的英文字母 CKEDITOR.replace('text', { filebrowserImageUploadUrl : 'upload_img.do', langu ...

  8. HTML5 上传图片预览

    html5出现之前如果需要上传图片预览 一般都是先上传到服务器然后远程预览 html5出现之后   有个filereader 解决了这问题 //选中图片之后 $("#fileAddPic&q ...

  9. [ASP.NET]使用uploadify上传图片,并在uploadify按钮上生成预览图

    目录 需求 主要代码 总结 需求 项目中有用到uploadify上传插件,给的原型就是上传成功后替换原来的图片.没办法需求在那儿,也不能修改需求吧,只能想办法解决问题了. 主要代码 修改uploadi ...

随机推荐

  1. Paxos算法

    Paxos算法是分布式系统中常用的一个保持系统一致性的算法,由美国计算机科学家Leslie B. Lamport提出.原文链接. 今天特意学习了一下Paxos的原理,为防忘记,记录下来.(看了的东西没 ...

  2. browserify总结

    一.browserify 简介 browserify is a tool for compiling node-flavored commonjs modules for the browser. Y ...

  3. java 读写锁

    http://tutorials.jenkov.com/java-concurrency/read-write-locks.html 翻译 读写锁比LOCK的实现更复杂,想象有一个应用程序能读和写一些 ...

  4. 非常出色的一款WinForm窗体重绘GUI类库源码

    基本控件的演示 ScrollBar滚动条 各种圆形进度条 ProgressBar进度条 Mdi演示,仿谷歌浏览器 多种皮肤可供选择 一套专业级别的GUI控件,目前包含了窗体.进度条.滚动条以及MDI多 ...

  5. github项目filter_firewall说明

    本文编写的目的: 本文是对上传到github上的项目进行说明.github链接:filter_firewall有任何意见或者建议可以Email:18277973721@sina.cn 项目介绍: 包过 ...

  6. soinn

    Growing Cell Structures A Self-Organizing Network for Unsupervised and Supervised Learning Here, and ...

  7. 宝马-中国官方网站服务站点信息爬去记录(解析json中数据)

    具体步骤: 1.进入宝马官网,查找经销商查询界面 http://www.bmw.com.cn/cn/zh/general/dealer_locator/content/dealer_locator.h ...

  8. 父子进程间通信模型实现(popen)

    0.FILE *popen(const char *command, const char *type); popen 函数相当于做了以下几件事: 1.创建一个无名管道文件 2. fork() 3.在 ...

  9. 制作滚动视图(ScrollView)

    怎样判断是否应当使用滚动视图 所谓的滚动视图,是指一个可以滑动的视窗,视窗大小和位置固定不变,视窗内的内容用户可以通过手指滑动或者拖动滚动天来进行滚动浏览. 滚动视图的目的是为了解决同类内容过多,一个 ...

  10. 微软职位内部推荐-Principal Dev Manager for Windows Phone Apps

    微软近期Open的职位: Location: China, BeijingDivision: Operations System Group Engineering Group OverviewOSG ...