java多附件上传 实例demo
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title></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=""><script type="text/javascript" src="../js/swfupload/js/swfupload.js"></script><script type="text/javascript" src="../js/swfupload/js/swfupload.queue.js"></script><script type="text/javascript" src="../js/swfupload/js/fileprogress.js"></script><script type="text/javascript" src="../js/swfupload/js/handlers.js"></script><style type="text/css">a{color:blue;text-decoration: underline;font-size: 13px;}</style><script type="text/javascript">var upload;window.onload = function() {upload = new SWFUpload({// 处理文件上传的url
    upload_url: "${pageContext.request.contextPath}/fileAction!uploadBatch.do",postName : "Filedata",//上传的file// 上传文件限制设置
    file_size_limit : "5MB",//指定要上传的文件的最大体积,可以带单位,合法的单位有:B、KB、MB、GB,默认为KB
    file_types : "*.jpg;*.jpeg;*.png;*.gif;*.bmp;",    //此处也可以修改成你想限制的类型,比如:*.doc;*.wpd;*.pdf
    file_types_description : "Word,Excel,Images Files",file_upload_limit : "200",//允许同时上传文件的个数
    file_queue_limit : "0",//允许队列存在的文件数量,默认值为0,即不限制// 事件处理设置(所有的自定义处理方法都在handler.js文件里)
    file_dialog_start_handler : fileDialogStart,file_queued_handler : fileQueued,file_queue_error_handler : fileQueueError,file_dialog_complete_handler : fileDialogComplete,//这里为自动上传
    upload_start_handler : uploadStart,upload_progress_handler : uploadProgress,upload_error_handler : uploadError,upload_success_handler : uploadSuccess,upload_complete_handler : uploadComplete,// 按钮设置
    button_image_url : "../js/swfupload/images/XPButtonNoText_61x22.png",    // 按钮图标
    button_placeholder_id : "spanButtonPlaceholder",button_width: 61,button_height: 22,button_text: '&nbsp;&nbsp;浏览...',// swf设置
    flash_url : "../js/swfupload/js/swfupload.swf",custom_settings : {progressTarget : "fsUploadProgress",cancelButtonId : "btnCancel"},removeCompleted : true,//上传完成后自动删除队列
        use_query_string : true,//要传递参数,必须配置//debug模式,可以在页面看到详细信息
        debug: false});};//重写上传成功方法function uploadSuccess(file, serverData) {try {var progress = new FileProgress(file, this.customSettings.progressTarget);var showMsg = "<span style='margin-left:100px'></span><font color='green'>---------------上传成功,</font><a style='text-decoration: underline;color:blue;' target='_blank' href='<%=request.getAttribute("loanDownloadPath")%>" + serverData.split(":")[1] + "'>预 览</a>       <img title='删除' οnclick='removeFile4Add(this,\""+ serverData.split(":")[1] +"\",\""+serverData.split(":")[0]+"\")' style='cursor: pointer;' src='../images/list_remove.png' />";progress.setStatus(showMsg);progress.toggleCancel(false);var fileObj = document.getElementById("filesLongString");fileObj.value = fileObj.value + serverData + "/";//alert(fileObj.value.length);
} catch (ex) {this.debug(ex);}}//上传成功后的删除function removeFile4Add(fileIdObj,newName,oldName) {$.messager.confirm("删除","确认删除该附件?",function(r){//jquery easyui 提示框if(r) {$(fileIdObj).parent().parent().parent().hide();//隐藏本行//转义括号和/var replaceStr = oldName + ":" + newName + "\/";replaceStr = replaceStr.replace(/\(/,"\\\(");replaceStr = replaceStr.replace(/\)/,"\\\)");replaceStr = replaceStr.replace(/\(/,"\\\(");replaceStr = replaceStr.replace(/\)/,"\\\)");var reg2 = new RegExp(replaceStr,'ig');var fileObj = document.getElementById("filesLongString");var result = fileObj.value.replace(eval(reg2),"");document.getElementById("filesLongString").value = result;}});}</script></head><body><%          /*循环上传后的附件并显示*/String files_oldname = (String)request.getAttribute("bean.files_oldname");String files_newname = (String)request.getAttribute("bean.files_newname");String loanDownloadPath = (String)request.getAttribute("downloadPath");String oldNameArr[] = null, newNameArr[] = null;if(files_oldname!=null && files_newname!=null) {oldNameArr = files_oldname.split("/");newNameArr = files_newname.split("/");}String filesLongString = "";//组装成新增借款时附件的格式//中文名.jpg:201506231401577954808.jpg/Map<String,String> fileMap = new TreeMap<String,String>();//有序if(oldNameArr!=null && newNameArr!=null) {for(int i=0; i<oldNameArr.length; i++) {fileMap.put(oldNameArr[i], newNameArr[i]);filesLongString = filesLongString + oldNameArr[i] + ":" + newNameArr[i] + "/";}}%><%int i = 0;for(Map.Entry<String, String> entry : fileMap.entrySet()) {i++;%><div id="fileId<%=i%>" style="padding-top:15px"><a target="_blank" href="<%=loanDownloadPath%><%=entry.getValue()%>"><%=entry.getKey() %></a><s:if test="#session.s_userRole==1"><%if(entry!=null && !"".equals(entry.getValue())){%><img title="删除" style="cursor: pointer;" src="../images/remove.png" onclick="removeFile('fileId<%=i%>','<%=entry.getValue()%>','<%=entry.getKey() %>')"><%}%></s:if></div><%}if(fileMap.size()==0) {//out.print("没有上传附件");}%><table><tr><td colspan="4" class="sm"><form id="filesForm" name="filesForm" method="post" action="" enctype="multipart/form-data"><s:token /><div class="flash" id="fsUploadProgress"></div><br/><div style="padding-left: 5px;"><font color="blue">请选择需要上传的附件:</font><span id="spanButtonPlaceholder"></span><!-- 不能去掉'取消'按钮,否则无法上传 --><input id="btnCancel" type="button" value="取消" onclick="cancelQueue(upload);" disabled="disabled" style="display:none;margin-left: 2px; height: 22px; font-size: 8pt;" /><input id="clearDivId" type="button" value="清空" onclick="clear();" style="display:none"/><br/>(提示:选择文件后会自动上传。)</div><input type="text" style="width: 500px; visibility: hidden;" id="filesLongString" name="filesLongString" value="<%=filesLongString%>"></form></td></tr></table></body>
</html>

//上传的文件到服务器
public void uploadBatch() {MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();File[] files = wrapper.getFiles("Filedata");File savefile = null;String realpath = ServletActionContext.getServletContext().getRealPath("/upload/");//上传路径int randomNum = (int) (Math.random() * 10000);// 生成一个10000以内的随机数SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");String fileNameStr = wrapper.getFileNames("Filedata")[0];//原名称String type = fileNameStr.substring(fileNameStr.lastIndexOf("."), fileNameStr.length());// 获取文件类型String newFileName = sdf.format(new Date()) + randomNum + type;// 当前时间作为上传后的文件名savefile = new File(new File(realpath), newFileName);savefile.setWritable(true, false);//设置可写,针对linux系统if (!savefile.getParentFile().exists()) {savefile.getParentFile().mkdirs();}try {FileUtils.copyFile(files[0], savefile);} catch (Exception e) {logger.error("上传excel异常:", e);}try {HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html; charset=UTF-8");response.setContentType("application/json;charset=UTF-8");response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);PrintWriter writer = response.getWriter();writer.print(fileNameStr + ":" + newFileName);//数据导入成功后,返回文件原名、现名到前台
            writer.flush();writer.close();} catch (IOException e) {logger.error(e);}}

posted on 2015-07-16 13:51 Stanley_yu 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/yushouling/p/4651106.html

java多附件上传 实例demo相关推荐

  1. JAVA图片裁剪上传实例______软件开发-帮助类

    实现思路 使用jcrop插件手机要裁剪图片的坐标 将收集到的参数传递到后台,在后台使用java图形对象绘制图像进行裁剪  后台处理流程:  1.将上传的图片按按照比例进行压缩后上传到文件服务器,并且将 ...

  2. httpclient 多附件上传

    多附件上传实例: /*** 多附件上传* @param host* @param uri* @param attachment 附件* @param param body参数* @return*/pu ...

  3. java 图片上传_java web图片上传和文件上传实例

    本篇文章主要介绍了java web图片上传和文件上传实例,具有一定的参考价值,有需要的可以了解一下. 图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多 ...

  4. java的图片上传与前端展示实例(Servlet+Jsp)

    java的图片上传与前端展示实例(Servlet+Jsp) 内容前瞻 1.关于路径 2.前端图片上传与展示 3.后台获取并保存 环境 1.前端jsp 2.服务器tomcat 3.服务端servlet ...

  5. java上传文件限制大小_Java Web:附件上传,两种文件上传限制格式及大小方法,学习笔记文件操作【诗书画唱】...

    附件上传: package com.jy.controller; import java.io.File; import java.io.FileOutputStream; import java.i ...

  6. java文件流 m.jb51.net_FasfDFS整合Java实现文件上传下载功能实例详解

    今天使用Java代码实现文件的上传和下载.对此作者提供了Java API支持,下载fastdfs-client-java将源码添加到项目中.或者在Maven项目pom.xml文件中添加依赖 org.c ...

  7. java操作hadoop hdfs,实现文件上传下载demo

    本文主要参考了Hadoop HDFS文件系统通过java FileSystem 实现上传下载等,并实际的做了一下验证.代码与引用的文章差别不大,现列出来作为备忘. import java.io.*; ...

  8. java web文件上传详解_java web图片上传和文件上传实例详解

    java web图片上传和文件上传 图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定 ...

  9. ajax图片上传插件demo,jQuery 自制上传头像插件-附带Demo实例(ajaxfileupload.js第三弹)...

    这篇文章主要是对前两篇关于ajaxfileupload.js插件的文章 的一个收关.但是最初也是因为想做这么一个功能,一点一点的引发出了好多问题,不断去学习,研究,才写了这三篇. 早些时候已经实现了上 ...

最新文章

  1. openstack云主机无法绑定ip_智汇华云|OpenStack 虚拟机 GPU 性能优化
  2. 吴恩达深度学习笔记1-Course1-Week1【深度学习概论】
  3. Open*** 安装脚本
  4. 计算机应用领域的CIMS,计算机的应用领域
  5. 字符串驻留机制截图?#注意回顾字符串的深浅拷贝小数据池那节
  6. Visual Studio 2010 sp1
  7. 上午写了一段代码,下午就被开除了,奇怪的知识又增加了
  8. Zabbix 服务器性能指标参考(学习笔记十七)
  9. 恭祝月圆·家圆·团圆
  10. fjnuoj 1004 游戏 (博弈论)
  11. 制作U盘安装UBUNTU
  12. 新能源汽车前景广袤但痛点多多,大圆柱电池能否提供最优解?
  13. allegro怎么导出PCB文件里封装
  14. 中文语音软件再次升级,即将发布百度语音助手
  15. 抖音测试距离的软件,抖音同款距离测量仪
  16. 香橙派 One Plus 像单片机一样硬件寄存器 控制GPIO 点灯
  17. 2019年DevOps最新现状研究报告解读
  18. 从零开始学Java编程!java数据结构面试题
  19. 自建 Bitwarden 服务器
  20. 图灵C/C++图书阅读路线图

热门文章

  1. [Scoi2015]小凸玩矩阵
  2. ubuntu无限卡在logo界面
  3. 学会使用Chromium中的LOG
  4. Python学习笔记4—Python字典元组
  5. 物联网概念升级,万物互联来袭
  6. 自定义用户验证控件CustomValidator
  7. XDP/eBPF — 基于 eBPF 的 Linux Kernel 可观测性
  8. etcd — 安装部署
  9. Go 语言编程 — panic 和 recover
  10. 4G EPS 中建立 UE 和 MME 之间的 NAS(非接入服务)信令连接