文件的上传(可以上传照片,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了,当报表格式非常负责的时候 开发难度会加大 假设报表有格式有变化 那就更复杂了,先发现一个非常老的技术.能够解决格式复杂的报表. 实例代码例如以下: <%@ ...
随机推荐
- 无需编写代码,用接口管理工具 eoLinker 高效完成API测试流程相关业务
引言 作为开发人员,有时需要协助市场部门进行开发,比如在市场推广中,经常通过给用户赠送优惠券来提高复购率.这篇文章,将介绍如何使用接口管理工具 eoLinker 的自动化测试 [UI模式] 构建此业务 ...
- 手把手从python安装到setuptools、pip工具安装
一.python安装1.基础开发库 apt-get install gccapt-get install openssl libssl-dev 2.安装数据库和开发库 apt-get install ...
- Asp.Mvc 常用
url转义 var address = "http://www.cnblog.com"; var a22 = Uri.EscapeDataString(address); var ...
- 28 I/O限制的异步操作
28.2 C#的异步函数 private static async Task<string> IssueClientRequestAsync(string serverName, stri ...
- dp有哪些种类
dp有哪些种类 一.总结 一句话总结: 二.dp动态规划分类详解 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. * ...
- 举枪消灭"烂代码"的实战案例
前言 之前我写过一篇如何少写PHP "烂"代码 https://segmentfault.com/a/11...感觉很多新人对此不太理解.今天以打卡功能为例,去讲解其中的奥秘.那篇 ...
- zend studio【快捷键】
=================================[快捷键 zend studio]========== 1.调出查找面板[ctrl+f] 2.全文检索[ctrl+h] 3.关闭当前文 ...
- linux的ulimit各种限制之深入分析
一般可以通过ulimit命令或编辑/etc/security/limits.conf重新加载的方式使之生效 通过ulimit比较直接,但只在当前的session有效,limits.conf中可以根据用 ...
- SSM框架下实现导入功能
何叫导入?简单说,导入就是把excel表格里的数据插入到数据库里.我这里做的是支持.xls和.xlsx格式. 因为是自己做案例记录,那些jar包什么的就不细说了,主要讲实现和步骤,看代码: 先在你的项 ...
- [bzoj3207]花神的嘲讽计划Ⅰ[可持久化线段树,hash]
将每k个数字求一个哈希值,存入可持久化线段树,直接查询即可 #include <iostream> #include <algorithm> #include <cstd ...