- 浏览: 31297 次
- 性别:
- 来自: 沈阳
最新评论
JSP代码
$(document).ready(function() {
});
$.extend({
excelUploadify : function (obj, importType, buttonText) {
$(obj).uploadify({
'uploader' : "js/uploadify/uploadify.swf",
'script' : "<%=basePath%>/InformationUpload.do",
'cancelImg' : "js/uploadify/cancel.png",
'auto' : true,
'multi' : false,
'queueID' : "fileQueue",
'fileDataName' : "upload",
'fileExt' : "*.xls; *.doc; *.pdf; *.ppt; *.docx; *.xlsx; *.pptx; *.txt; *.jpg; *.png; *.gif; *.mts; *.mpg",
'fileDesc' : "支持文件; *.xls; *.doc; *.pdf; *.ppt; *.docx; *.xlsx; *.pptx; *.txt; *.jpg; *.png; *.gif; *.mts; *.mpg",
'sizeLimit' : '104857600',
'buttonText' : buttonText,
'scriptData' : {'importType' : importType},
'onSelect' : function(event,ID,fileObj) {
//解决中文问题
$('#upload').uploadifySettings('scriptData',{'filename': encodeURI(encodeURI(fileObj.name)),'type' : 'upload'});
},
'onComplete' : function(event, queueID, fileObj, response, data) {
}
});
}
});
<!-- 上传 -->
<div style="float:right;margin-right:10px;background-image:none; background-color:none;">
<table width="20" border="0" align="right">
<tr>
<td>
<div align="right" >
<input id="upload" name="upload" type="file" style="background-image:none; background-color:none;" />
</div>
<div id="fileQueue" align="left"></div>
</td>
</tr>
</table>
</div>
后台:
package cn.com.sundy.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.com.sundy.manager.ConverterService;
import cn.com.sundy.util.ImageThumb;
import cn.com.sundy.util.TimeSource;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author WangMeng
*/
@Scope("prototype")
@Controller("informationUploadAction")
public class InformationUploadAction extends ActionSupport implements ServletContextAware,RequestAware,SessionAware,ServletResponseAware{
private static Logger log = Logger.getLogger(InformationUploadAction.class);
private Map<String,Object> request;
private Map<String,Object> session;
private HttpServletResponse response;
private ServletContext servletContext;
private String[] upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
private String savePath;
private String serverSavedName;
private String importType;
private String saveDir = "file\\temp\\upload\\data";
//文件分割符
private String seprater="\\";
@Autowired
ConverterService converterService;
public Map<String, Object> getRequest() {
return request;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
/**
* 上传
*
* @return
* @throws Exception
*/
public String upload() throws Exception {
//设置回写部分
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
// 判断是否允许上传
String[] allowedTypes = getAllowTypes().split(",");
String filterResult = filterType(allowedTypes);
if (filterResult != null) {
return filterResult;
}
String randomTimeMillis = TimeSource.getNowTime14() + String.valueOf(System.currentTimeMillis());
String name1 = java.net.URLDecoder.decode(ServletActionContext.getRequest().getParameter("filename"), "utf-8").toLowerCase();
this.setUploadFileName(this.generatFileName(name1, randomTimeMillis));
// 以服务器的文件保存地址和原文件名建立上传文件输出流
File source = new File(getUpload()[0]);
serverSavedName = getSavePath() + "\\" + getUploadFileName();
String pdfName = serverSavedName.substring(0, serverSavedName.lastIndexOf(".")) + ".pdf";
//随机生成存到数据库中
String swfName = randomTimeMillis + ".swf";
PrintWriter out = response.getWriter();
try{
File serverSavedFile = new File(serverSavedName);
if (!serverSavedFile.exists() || !serverSavedFile.isFile()) {
File parent = serverSavedFile.getParentFile();
if (!parent.exists() || !parent.isDirectory()) {
parent.mkdirs();
}
serverSavedFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(serverSavedFile);
FileInputStream fileInputStream = new FileInputStream(source);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
}
}catch(IOException e){
out.println("<script>alert('上传失败')</script>");
out.flush();
out.close();
return null;
}
String user = (String) session.get("username");
//上传人,上传时间,文件路径
out.print(user + "," + TimeSource.getNowTime19() + "," + this.saveDir + this.seprater + name1 + "," + this.saveDir + this.seprater + swfName + "," + this.saveDir + this.seprater + this.getUploadFileName() + "," + this.saveDir + this.seprater + SaveImageThumbName + "," + this.saveDir + this.seprater + SaveImageIndexName);
out.flush();
out.close();
return null;
}
/**
* 生成新文件名
* @param fileName
* @param randomTimeMillis
* @return
*/
public String generatFileName(String fileName, String randomTimeMillis){
String randomFileName = fileName.substring(0, fileName.trim().lastIndexOf(".")) + randomTimeMillis + fileName.substring(fileName.lastIndexOf("."));
return randomFileName;
}
/************************ Assistant ***********************/
/**
* 过滤上传类型
*
* @param types
* @return
*/
public String filterType(String[] types) {
String fileType = this.getUploadContentType();
System.out.println("fileType:" + fileType);
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
}
/************************ Getter & Setter ***********************/
public String[] getUpload() {
return upload;
}
public void setUpload(String[] upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) throws UnsupportedEncodingException {
this.uploadFileName = uploadFileName;
}
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
public String getSavePath() {
return servletContext.getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getServerSavedName() {
return serverSavedName;
}
public void setServerSavedName(String serverSavedName) {
this.serverSavedName = serverSavedName;
}
@Override
public void setServletContext(ServletContext arg0) {
this.servletContext = arg0;
}
public String getImportType() {
return importType;
}
public void setImportType(String importType) {
this.importType = importType;
}
public ServletContext getServletContext() {
return servletContext;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
public String getSaveDir() {
return saveDir;
}
public void setSaveDir(String saveDir) {
this.saveDir = saveDir;
}
public String getSeprater() {
return seprater;
}
public void setSeprater(String seprater) {
this.seprater = seprater;
}
}
//struts
<action name="InformationUpload" class="informationUploadAction" method="upload" >
<param name="allowTypes">
application/msexcel,application/vnd.ms-excel,application/octet-stream <!-- Separated by ',' -->
</param>
</action>
$(document).ready(function() {
[/align][align=left]
$.excelUploadify("#upload", "information", "上传文件");
});
$.extend({
excelUploadify : function (obj, importType, buttonText) {
$(obj).uploadify({
'uploader' : "js/uploadify/uploadify.swf",
'script' : "<%=basePath%>/InformationUpload.do",
'cancelImg' : "js/uploadify/cancel.png",
'auto' : true,
'multi' : false,
'queueID' : "fileQueue",
'fileDataName' : "upload",
'fileExt' : "*.xls; *.doc; *.pdf; *.ppt; *.docx; *.xlsx; *.pptx; *.txt; *.jpg; *.png; *.gif; *.mts; *.mpg",
'fileDesc' : "支持文件; *.xls; *.doc; *.pdf; *.ppt; *.docx; *.xlsx; *.pptx; *.txt; *.jpg; *.png; *.gif; *.mts; *.mpg",
'sizeLimit' : '104857600',
'buttonText' : buttonText,
'scriptData' : {'importType' : importType},
'onSelect' : function(event,ID,fileObj) {
//解决中文问题
$('#upload').uploadifySettings('scriptData',{'filename': encodeURI(encodeURI(fileObj.name)),'type' : 'upload'});
},
'onComplete' : function(event, queueID, fileObj, response, data) {
}
});
}
});
<!-- 上传 -->
<div style="float:right;margin-right:10px;background-image:none; background-color:none;">
<table width="20" border="0" align="right">
<tr>
<td>
<div align="right" >
<input id="upload" name="upload" type="file" style="background-image:none; background-color:none;" />
</div>
<div id="fileQueue" align="left"></div>
</td>
</tr>
</table>
</div>
后台:
package cn.com.sundy.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.com.sundy.manager.ConverterService;
import cn.com.sundy.util.ImageThumb;
import cn.com.sundy.util.TimeSource;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author WangMeng
*/
@Scope("prototype")
@Controller("informationUploadAction")
public class InformationUploadAction extends ActionSupport implements ServletContextAware,RequestAware,SessionAware,ServletResponseAware{
private static Logger log = Logger.getLogger(InformationUploadAction.class);
private Map<String,Object> request;
private Map<String,Object> session;
private HttpServletResponse response;
private ServletContext servletContext;
private String[] upload;
private String uploadContentType;
private String uploadFileName;
private String allowTypes;
private String savePath;
private String serverSavedName;
private String importType;
private String saveDir = "file\\temp\\upload\\data";
//文件分割符
private String seprater="\\";
@Autowired
ConverterService converterService;
public Map<String, Object> getRequest() {
return request;
}
public void setRequest(Map<String, Object> request) {
this.request = request;
}
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> session) {
this.session = session;
}
/**
* 上传
*
* @return
* @throws Exception
*/
public String upload() throws Exception {
//设置回写部分
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
// 判断是否允许上传
String[] allowedTypes = getAllowTypes().split(",");
String filterResult = filterType(allowedTypes);
if (filterResult != null) {
return filterResult;
}
String randomTimeMillis = TimeSource.getNowTime14() + String.valueOf(System.currentTimeMillis());
String name1 = java.net.URLDecoder.decode(ServletActionContext.getRequest().getParameter("filename"), "utf-8").toLowerCase();
this.setUploadFileName(this.generatFileName(name1, randomTimeMillis));
// 以服务器的文件保存地址和原文件名建立上传文件输出流
File source = new File(getUpload()[0]);
serverSavedName = getSavePath() + "\\" + getUploadFileName();
String pdfName = serverSavedName.substring(0, serverSavedName.lastIndexOf(".")) + ".pdf";
//随机生成存到数据库中
String swfName = randomTimeMillis + ".swf";
PrintWriter out = response.getWriter();
try{
File serverSavedFile = new File(serverSavedName);
if (!serverSavedFile.exists() || !serverSavedFile.isFile()) {
File parent = serverSavedFile.getParentFile();
if (!parent.exists() || !parent.isDirectory()) {
parent.mkdirs();
}
serverSavedFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(serverSavedFile);
FileInputStream fileInputStream = new FileInputStream(source);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}
fileInputStream.close();
fileOutputStream.close();
}
}catch(IOException e){
out.println("<script>alert('上传失败')</script>");
out.flush();
out.close();
return null;
}
String user = (String) session.get("username");
//上传人,上传时间,文件路径
out.print(user + "," + TimeSource.getNowTime19() + "," + this.saveDir + this.seprater + name1 + "," + this.saveDir + this.seprater + swfName + "," + this.saveDir + this.seprater + this.getUploadFileName() + "," + this.saveDir + this.seprater + SaveImageThumbName + "," + this.saveDir + this.seprater + SaveImageIndexName);
out.flush();
out.close();
return null;
}
/**
* 生成新文件名
* @param fileName
* @param randomTimeMillis
* @return
*/
public String generatFileName(String fileName, String randomTimeMillis){
String randomFileName = fileName.substring(0, fileName.trim().lastIndexOf(".")) + randomTimeMillis + fileName.substring(fileName.lastIndexOf("."));
return randomFileName;
}
/************************ Assistant ***********************/
/**
* 过滤上传类型
*
* @param types
* @return
*/
public String filterType(String[] types) {
String fileType = this.getUploadContentType();
System.out.println("fileType:" + fileType);
for (String type : types) {
if (type.equals(fileType)) {
return null;
}
}
return INPUT;
}
/************************ Getter & Setter ***********************/
public String[] getUpload() {
return upload;
}
public void setUpload(String[] upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) throws UnsupportedEncodingException {
this.uploadFileName = uploadFileName;
}
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
public String getSavePath() {
return servletContext.getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getServerSavedName() {
return serverSavedName;
}
public void setServerSavedName(String serverSavedName) {
this.serverSavedName = serverSavedName;
}
@Override
public void setServletContext(ServletContext arg0) {
this.servletContext = arg0;
}
public String getImportType() {
return importType;
}
public void setImportType(String importType) {
this.importType = importType;
}
public ServletContext getServletContext() {
return servletContext;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response=arg0;
}
public String getSaveDir() {
return saveDir;
}
public void setSaveDir(String saveDir) {
this.saveDir = saveDir;
}
public String getSeprater() {
return seprater;
}
public void setSeprater(String seprater) {
this.seprater = seprater;
}
}
//struts
<action name="InformationUpload" class="informationUploadAction" method="upload" >
<param name="allowTypes">
application/msexcel,application/vnd.ms-excel,application/octet-stream <!-- Separated by ',' -->
</param>
</action>
- uploadify.rar (49.1 KB)
- 下载次数: 8
发表评论
-
百度地图和ztree开发电子围栏功能
2016-09-29 09:27 4682jsp <%@ page language=" ... -
Oracle11g Enterprise Manager配置失败
2014-04-29 11:20 2532Win7先安装oracle 11g时,安装检测到系统的主 IP ... -
java弹窗导入导出文件
2014-03-24 11:42 1448package com.action.frame; impo ... -
jsp访问上级窗口元素
2014-04-29 11:22 418opener.document.getElementById( ... -
页面关闭IE触发事件方法
2013-09-30 09:56 554function window.onbeforeunload( ... -
页面右下角消息提示
2013-09-29 14:05 470<%@ page language="java ... -
oracle 日期列表
2013-09-29 12:52 482获取某个时间段之间的月份列表(示例返回2009-03到2010 ... -
倒计时
2013-09-24 10:01 321//提交 function doSubmit(){ ... -
oracle11G 最新驱动包
2013-09-24 10:00 678oracle11G 最新驱动包 解决与Spring冲突问题 ... -
获取第几天后的日期
2013-09-16 09:23 559Calendar cal = Calendar.getInst ... -
web工程下获取properties文件
2013-09-16 09:19 543String propertiesPath = getClas ... -
javamail
2013-09-16 09:15 532final SendMail sendmail = new S ... -
java根据年和周获取当前的日期时间段
2013-09-16 09:10 1252String[] dates = new String[7]; ... -
JSP判断日期间隔天数
2013-07-15 11:14 602var temp_field1 = $("#temp ... -
dtree不选中子节点处理
2013-05-17 10:35 1244//dtree.js 208行(chks[i].checked ... -
IE8下JS插入表格
2013-05-17 10:25 631//IE8下使用JS插入JSP表格 var tbl = do ... -
JSP选项卡显示
2013-05-17 10:22 1996<%@ page language="java ... -
JSP样式实例
2013-05-10 15:59 643<%@ page contentType="t ... -
Oracle表空间和用户创建赋权限
2013-05-10 15:54 635--RLDM CREATE TABLESPACE RLDM D ... -
hibernate oracle 主键递增
2013-05-10 15:47 464<?xml version="1.0" ...
相关推荐
HTML5 PHP jquery uploadify上传文件,带进度条,author:吕大豹。仿照uploadify写的,www.codesc.net已做过修正,本例的配置参数均与uploadify官网一致,参照官网的api就可以了,直接把文件夹上传到支持php的服务器...
struts2 + uploadify上传文件示例
MVC4下对话框中使用Uploadify上传多个文件
使用Jquery.uploadify上传文件。 jquery有很多插件,其中也有不少上传文件的插件,像ajaxfileupload.js、uploadify.js,不过网上的例子几乎全是php的,很郁闷,研究了半天,终于把uploadify用jsp版本弄出来了
jquery-java-ajax-uploadify上传文件
springMVC+uploadify上传图片。
uploadify上传文件 uploadify实现多文件的上传 带滚动的进度条
该资源包含了一个使用uploadify组件上传文件的简单demo,喜欢的下载。
uploadify多文件上传例子代码,jsp+servlet实现
NULL 博文链接:https://amandazzu.iteye.com/blog/1428775
C# uploadify文件上传,简单实用
jquery.uploadify同时上传多个文件
PHP+UploadiFy批量上传文件,需要的小伙伴过来看下,相互学习
Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示,可多文件上传
Java调用uploadify实现文件上传Demo实现,文档参见:http://www.mbaike.net/java/1940.html
Uploadify是JQuery的一个上传插件,实现的效果非常不错,...接下来通过本文给大家介绍Uploadify上传文件方法,涉及到Uploadify在Aspnet中的使用相关知识,本文介绍的非常详细,具有参考借鉴价值,需要的朋友一起学习吧
uploadify批量上传文件-
MVC 上传文件 uploadify …………………………………………………………………………………………………………………………………………………………………………………………………………………………………………...
jquery文件上传插件 jquery.uploadify.js 不支持IE10现在经过修改,已经支持IE6、IE7、IE8、IE9、IE10、IE11.zip
NULL 博文链接:https://rox.iteye.com/blog/834595