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. php第十九节课

    JQUERY <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  2. [JS]window.location获取url各项参数详解

    window.location方法后还还可以带href,search等参数,下面我们来看看获取url各项参数的办法. URL即:统一资源定位符 (Uniform Resource Locator, U ...

  3. 洛谷——P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题意翻译 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  4. assertion: 18 { code: 18, ok: 0.0, errmsg: "auth fails" }

    MongoDB Version: 2.4.7 Mongodump: $ bin/mongodump -u admin -p admin -d test -o ./tmp/ connected to: ...

  5. N天学习一个linux命令之kill

    用途 用于终止进程 用法 kill [-s signal|-p] [--] pid... kill -l [signal] 说明 1.默认发送信号15(请求终止进程,程序可以捕获,操作系统会杀死没有对 ...

  6. [Cypress] Interact with Hidden Elements in a Cypress Test

    We often only show UI elements as a result of some user interaction. Cypress detects visibility and ...

  7. WebGIS开发技术杂谈

    WebGIS项目的开发主要是B/S架构.最流行的是clientjavascript,server端java. 另外还有flexclient. client主要完毕用户交互.向server端发送请求并传 ...

  8. Cocoa pods的安装和使用

    现在网上关于cocoapods的安装使用资料有很多,有些方法能用,有些是用不了的,别问为什么,因为我就是从坑里走出来的.在此自己整理了一些方法: 一般需要先升级Ruby环境: 第一步:安装rvm $  ...

  9. 大数据处理之道(实验方法&lt;二&gt;)

    一:交叉验证(crossvalidation)(附实验的三种方法)方法简单介绍   (1) 定义:交叉验证(Cross-validation)主要用于建模应用中,比如PCR(Principal Com ...

  10. 本地项目上传虚拟机的gitlab

    前提:在虚拟机安装了gitlab服务,并且本机可以访问到虚拟机的gitlab 自己本机项目上传到gitlab 1.先在gitlab上建立项目 拷贝项目地址: http://192.168.1.105/ ...