文件的上传(可以上传照片,word文档,等单个文件)
jsp:
jsp页面:
<LINK href="${basePath}plugins/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
<script language="javascript" type="text/javascript">
//防止客户端缓存文件,造成uploadify.js不更新,而引起的“喔唷,崩溃啦”
document.write ("<script type='text/javascript' " + "src='${basePath}plugins/uploadify/jquery.uploadify.min.js?" + new Date () + "'><\/script>");
</script>
<style>
.uploadify-box {
width: 130px;
margin: 0px;
margin-top: 10px;
}
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <script type="text/javascript">
<script type="text/javascript">
$(function() {
//文件上传
$('.uploadFile').each(function(){
var id= $(this).attr("id");
var $this = $(this);
$("#"+id).uploadify({
buttonClass:'',
fileSizeLimit: '20480KB',
uploader: '${basePath}core/upload!uploadFile.action',// 服务器端处理地址
swf: '${basePath}js/uploadify/uploadify.swf',// 上传使用的 Flash
buttonText: "上传文档",
buttonCursor: 'hand',
fileObjName: 'uploadify',// 上传参数名称 后台action里面的属性uploadify
fileTypeExts: "*.jpg;*.png;*.gif;*.pdf;*.doc;*.docx", // 扩展名
fileTypeDesc: "请选择 文件格式",
removeTimeout:, // 文件说明
auto: true,// 选择之后,自动开始上传
multi: false, // 是否支持同时上传多个文件
queueSizeLimit: ,// 允许多文件上传的时候,同时上传文件的个数
queueID:'queueID', onUploadSuccess:function(file, data, response){
var data =jQuery.parseJSON(data);
if(data.error == ){
ldDialog.alert(data.message);
}else{
$("#filePath").val(data.url);
$("#fileName").val(data.fileName);
$("#userFileName").html(data.fileName);
}
}
});
});
});
</script>
@RequestMapping("core/upload!uploadFile.action")
public void uploadFile(@RequestParam(value = "uploadify", required = false) MultipartFile uploadify,HttpServletRequest request,HttpServletResponse response,ModelMap modelMap) throws Exception {
@SuppressWarnings("deprecation")
String savePath = request.getRealPath("/")+ "/" + "upload/";
String saveUrl = "upload/";
HashMap<String, String> extMap = new HashMap<String, String>();
extMap.put("file", "pdf,rar,zip,7z,gif,jpg,jpeg,png,bmp,doc,docx,xlsx,xls");
//extMap.put("image", "gif,jpg,jpeg,png,bmp");
extMap.put("csv", "csv");
int uploadMaxSize =Integer.valueOf(optionService.getByOptionName(CoreValue.OPTION_UPLOAD_MAX_SIZE).getOptionValue());
long maxSize = Long.valueOf(String.valueOf(uploadMaxSize)).longValue();
String maxSizeKb = StringUtil.formatNumber(new Double(maxSize / 1024L),"");
response.setContentType("text/html; charset=UTF-8");
String jsonString = "";
if (!ServletFileUpload.isMultipartContent(getRequest())) {
jsonString = getError("请选择文件。");
response.getWriter().println(jsonString);
return;
}
File uploadDir = new File(savePath);
if (!uploadDir.isDirectory()) {
jsonString = getError(savePath + "上传目录不存在。");
response.getWriter().println(jsonString);
return;
}
if (!uploadDir.canWrite()) {
jsonString = getError("上传目录没有写权限。");
response.getWriter().println(jsonString);
return;
}
String dirName = getRequest().getParameter("dir");
if (dirName == null) {
dirName = "file";
}
if (!extMap.containsKey(dirName)) {
jsonString = getError("目录名不正确。");
response.getWriter().println(jsonString);
return;
}
savePath = savePath + dirName + "/";
saveUrl = saveUrl + dirName + "/";
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
String ymd = sdf.format(new Date());
savePath = savePath + ymd + "/";
saveUrl = saveUrl + ymd + "/";
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
String fileName = uploadify.getOriginalFilename();
long fileSize = uploadify.getSize();
if (fileSize > maxSize) {
jsonString = getError("上传文件大小超过限制。最大为" + maxSizeKb + "kb");
response.getWriter().println(jsonString);
return;
}
String fileExt = fileName.substring(fileName.lastIndexOf(".") + ).toLowerCase();
if (!Arrays.asList(((String) extMap.get(dirName)).split(",")).contains(fileExt)) {
jsonString = getError("不允许的上传文件类型。");
response.getWriter().println(jsonString);
return;
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_"+ new Random().nextInt() + "." + fileExt;
File uploadedFile = null;
try {
uploadedFile = new File(savePath, newFileName);
if(uploadedFile.exists()){
}
if(!uploadedFile.exists()){
uploadedFile.mkdirs();
}
uploadify.transferTo(uploadedFile);
} catch (Exception e) {
jsonString = getError("上传文件失败。");
response.getWriter().println(jsonString);
return;
}
JSONObject obj = new JSONObject();
obj.put("error", Integer.valueOf());
obj.put("url", saveUrl + newFileName);
obj.put("newFileName", newFileName);
obj.put("fileName", fileName);
jsonString = obj.toJSONString();
response.getWriter().println(jsonString);
}
private String getError(String message) {
JSONObject obj = new JSONObject();
obj.put("error", Integer.valueOf());
obj.put("message", message);
return obj.toJSONString();
}
文件的上传(可以上传照片,word文档,等单个文件)的更多相关文章
- 微信公众号怎么添加附件?比如word文档,pdf文件等
微信公众号怎么添加附件?比如word文档,pdf文件等 我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众 ...
- java word文档 转 html文件
一.简介 一般word文件后缀有doc.docx两种.docx是office word 2007以及以后版本文档的扩展名:doc是office word 2003文档保存的扩展名.对于这两种格式的wo ...
- POI实现word文档转html文件
POI word文件转html package com.feiruo.officeConvert; import java.io.BufferedWriter; import java.io.File ...
- 如何在PowerDesigner将PDM导出生成WORD文档或者html文件
a) 使用PowerDesigner打开pdm文件 b) 点击Report Temlates 制作模板 点击PowerDesigner菜单栏“Report” -> ...
- 批量转换word文档到pdf文件
最近在整理每周的工作记录.因为每周的工作记录大都是单独的word文件,有时候忘记了也不容易找出来,一个个打开查找太费劲,因此想着把这些文件通过word2016的另存为功能转换为pdf,然后永Acrob ...
- JAVA使用aspose实现word文档转pdf文件
引入jar包 下载地址:https://yvioo.lanzous.com/iezpdno3mob 然后打开下载的目录打开cmd执行 mvn install:install-file -Dfile=a ...
- Java 使用 jacob 将 word 文档转换为 pdf 文件
网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...
- 转换Word文档为PDF文件
1.使用 Office COM组件的Microsoft.Office.Interop.word.dll库 该方法需要在电脑上安装Office软件,并且需要Office支持转换为PDF格式,如果不支持, ...
- DEV word文档转换为pdf文件
引用aspose.net控件2.0. docement doc=new document(文件路径和名称); doc.save(输出路径\file.pdf);
- JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦
如今导出excel基本上都是用poi了,当报表格式非常负责的时候 开发难度会加大 假设报表有格式有变化 那就更复杂了,先发现一个非常老的技术.能够解决格式复杂的报表. 实例代码例如以下: <%@ ...
随机推荐
- JAVA学习总结-常用数据结构
java中集合框架其实就是数据结构的实现的封装; 参考资料:任小龙教学视频 1,什么是数据结构? 数据结构是计算机存储,组织数据的方式; 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合; ...
- 基础命令history
history 记录历史命令 环境变量: HISTSIZE: 命令历史记录的条数: HISTFILE: 命令历史记录的文件,~/.bash_history: HISTFILESIZE: 命令历史 ...
- 第十二节:pandas缺失数据处理
1.isnull():检查是否含有确实数据 2.fillna():填充缺失数据 3.dropna() :删除缺失值 4.replace():替换值
- Tensorflow人工智能入门(一)
前言: 作为一个程序员,已经离开开发岗好多年,最近突然迷茫了,不知道自己何去何从.互联网技术发展的速度已快得难以想象,许久不码代码的手也越来越僵直,需求沟通中的套话和空话却越发的熟练,这和当年入行时的 ...
- LDAP目录服务折腾之后的总结
前言 公司管理员工信息以及组织架构的后台系统要和Active Directory目录服务系统打通,后台系统使用PHP开发, 折腾了二十多天,终于上线了,期间碰到过各种疑难问题,不过总算在GOOGLE大 ...
- portmap 和 rpc程序
Portmap 是为RPC 程序服务的. 每一个RPC server程序启动的时候要向portmap程序注册.这样portmap程序就知道这些RPC server监听在哪个端口. 而RPC clien ...
- Clojure:导入lein项目到IntelliJ IDEA
首先,我们需要先创建一个lein项目(废话..) lein new [项目名称] 然后生成Maven的pom.xml文件 cd [项目目录] lein pom 最后,在InteliJ IDEA中选择导 ...
- PHP array_combine()
定义和用法 array_combine() 函数通过合并两个数组来创建一个新数组,其中的第一个数组是键(索引),第二个数组为值. 如果其中一个数组为空,或者两个数组的长度不同,则该函数返回 false ...
- XMPP基本内容简单介绍
即时通讯技术简单介绍 即时通讯技术(IM)支持用户在线实时交谈.假设要发送一条信息,用户须要打开一个小窗体,以便让用户及其朋友在当中输入信息并让交谈两方都看到交谈的内容.有很多的IM系统,如AOL I ...
- Shredding Company (hdu 1539 dfs)
Shredding Company Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...