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文档,等单个文件)的更多相关文章

  1. 微信公众号怎么添加附件?比如word文档,pdf文件等

    微信公众号怎么添加附件?比如word文档,pdf文件等   我们都知道创建一个微信公众号,在公众号中发布一些文章是非常简单的,但公众号添加附件下载的功能却被限制,如今可以使用小程序“微附件”进行在公众 ...

  2. java word文档 转 html文件

    一.简介 一般word文件后缀有doc.docx两种.docx是office word 2007以及以后版本文档的扩展名:doc是office word 2003文档保存的扩展名.对于这两种格式的wo ...

  3. POI实现word文档转html文件

    POI word文件转html package com.feiruo.officeConvert; import java.io.BufferedWriter; import java.io.File ...

  4. 如何在PowerDesigner将PDM导出生成WORD文档或者html文件

    a)         使用PowerDesigner打开pdm文件 b)         点击Report Temlates 制作模板 点击PowerDesigner菜单栏“Report” -> ...

  5. 批量转换word文档到pdf文件

    最近在整理每周的工作记录.因为每周的工作记录大都是单独的word文件,有时候忘记了也不容易找出来,一个个打开查找太费劲,因此想着把这些文件通过word2016的另存为功能转换为pdf,然后永Acrob ...

  6. JAVA使用aspose实现word文档转pdf文件

    引入jar包 下载地址:https://yvioo.lanzous.com/iezpdno3mob 然后打开下载的目录打开cmd执行 mvn install:install-file -Dfile=a ...

  7. Java 使用 jacob 将 word 文档转换为 pdf 文件

    网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...

  8. 转换Word文档为PDF文件

    1.使用 Office COM组件的Microsoft.Office.Interop.word.dll库 该方法需要在电脑上安装Office软件,并且需要Office支持转换为PDF格式,如果不支持, ...

  9. DEV word文档转换为pdf文件

    引用aspose.net控件2.0. docement doc=new document(文件路径和名称); doc.save(输出路径\file.pdf);

  10. JAVA原始的导出excel文件,快捷通用 方便 还能够导出word文档哦

    如今导出excel基本上都是用poi了,当报表格式非常负责的时候 开发难度会加大 假设报表有格式有变化 那就更复杂了,先发现一个非常老的技术.能够解决格式复杂的报表. 实例代码例如以下: <%@ ...

随机推荐

  1. Oracle,sqlserver,mySQl的区别和联系:

    1.日期处理方式 2.对保留字和关键字的处理方式: Oracle,sqlserver,mySQl的保留字不可以用作列字段,关键字可以,但他们对关键字的处理方式又不同: Oracle:关键字作为列时:用 ...

  2. Luogu 2951 捉迷藏Hide and Seek

    P2951 [USACO09OPEN]捉迷藏Hide and Seek 题目描述 Bessie is playing hide and seek (a game in which a number o ...

  3. 推荐系统相关比赛-kaggle

    from: 七月在线 电商推荐与销量预测相关案例 一.预测用户对哪个事件感兴趣(感兴趣不一定去参加) 用户历史参加事件.社交信息.浏览信息(app).要预测的事件 recall:召回率 准确率: 协同 ...

  4. Win32中 DLL、Lib 库的创建机器使用

    Windows 下 的静态库和动态库 一.静态函数库(Lib) 1. 静态函数库的制作(C/C++) —— 打开新建项目,然后选中Win32项目,接着在创建项目中选择 Lib,再接着将函数.实现功能的 ...

  5. idea和eclipse互相导入方法

    1.eclipse maven项目导入idea 只需要在pom.xml文件中加入如下图 为了加载项目里的一些配置文件(例如.xml和.properties文件) 2.idea maven 项目导入ec ...

  6. CentOS 7.2安装配置Vsftp服务器

    一.配置防火墙,开启FTP服务器需要的端口 CentOS 7.2默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  7. 13.multi_match实现dis_max+tie_breaker

    主要知识点: 基于multi_match语法实现dis_max+tie_breaker     1.best_fields+tie_breaker GET /forum/article/_search ...

  8. nagios监控ganglia指标,并配置告警

    背景:nagios的监控插件在轻量级方面不如ganglia的gmond,且ganglia在指标监控方面尤其突出的优势,而nagios在配置告警方面也比较方便,正好可以弥补ganglia无法配置告警通知 ...

  9. Java基础学习总结(41)——JPA常用注解

    JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.         JPA由EJB 3.0软件专 ...

  10. [置顶] Java基础学习总结(34)——HTTP协议详解

    一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...