uploadify java实现多文件上传和预览
作者:zz_cl 发布时间:2022-12-25 07:07:16
标签:java,上传,预览,uploadify
本文实例为大家分享了java文件上传和预览实现代码,供大家参考,具体内容如下
1、下载uploadify插件
2、index.html
<!DOCTYPE html>
<html lang="en">
<head>
<@head/>
<script src="<@path/>/js/uploadify-v3.1/jquery.uploadify-3.1.js"></script>
<link href="<@path/>/js/uploadify-v3.1/uploadify.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#uploader {
position: relative;
}
#uploader_queue {
position: absolute;
width: 600px;
left: 200px;
top: 0;
}
</style>
<script type="text/javascript">
$(function() {
$("#file_upload")
.uploadify(
{
'auto' : false,
'method' : "get",
'formData' : {
'folder' : 'file'
},
'height' : 30,
'swf' : '<@path/>/js/uploadify-v3.1/uploadify.swf', // flash
'uploader' : '<@path/>/uploadAttach.do', //
'width' : 120,
'fileTypeDesc' : 'ֻ支持多种文件格式',
'fileTypeExts' : '.dat;.264;.h264;.mp4;.dav;.MP4;.AVI;.ts;.avi;'
+ '.mpg;.rmvb;.flv;.rm;.mov;.wmv;.JPG;.bmp;.png;.BMP;.jpg;.PNG;'
+ '.gif;.xlsx;.xls;.txt;.pdf;.doc;.docx;.rar;.zip;.7z',
'fileSizeLimit' : '800KB',
'buttonText' : '选择文件',
'uploadLimit' : 5,
'successTimeout' : 5,
'requeueErrors' : false,
'removeTimeout' : 10,
'removeCompleted' : false,
'queueSizeLimit' : 10,
'queueID' : 'uploader_queue',
'progressData' : 'speed',
'onInit' : function() {
},
'onUploadSuccess' : function(file, data, response) {
$("#uploader_view").append(
'<img height="60" alt="" src="<@path/>/upload/'
+ encodeURI(data)
+ '"/><br/><br/>');
},
'onQueueComplete' : function(queueData) {
$('#uploader_msg').html(
queueData.uploadsSuccessful
+ '个文件上传成功<br/>');
}
});
});
</script>
</head>
<body class="">
<@header/>
<br />
<br />
<br />
<br />
<div id="uploader">
<p>
<input type="file" name="file_upload" id="file_upload" />
</p>
<a href="javascript:$('#file_upload').uploadify('upload','*')">上传</a>
<a href="javascript:$('#file_upload').uploadify('stop')">取消上传</a>
<div id="uploader_queue"></div>
<div id="uploader_msg"></div>
<div id="uploader_view"></div>
</div>
<br />
<br />
<br />
<br /> <@footer/>
</body>
</html>
3、java文件
package com.frame.core.ctrl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class loginCtrl {
private static Logger log = Logger.getLogger(loginCtrl.class);
@RequestMapping(value = "/goindex")
public ModelAndView goindex() {
ModelAndView mav = new ModelAndView("index");
mav.addObject("name", "笑傲江湖");
mav.addObject("projectName", "Freemarker框架");
return mav;
}
@RequestMapping(value = "/login")
public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
request.getSession().setAttribute("username", "身份认证成功");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
@RequestMapping("/uploadAttach")
public void processUploadDir(ModelMap modelMap,
MultipartHttpServletRequest request, PrintWriter writer) throws Exception {
Map<String, MultipartFile> fileMap = request.getFileMap();
String path = request.getSession().getServletContext().getRealPath("/");;
System.out.println("path:"+path);
Date currentTime = new Date();
long prefix = currentTime.getTime();
StringBuffer attachIds = new StringBuffer();
for (Map.Entry<String, MultipartFile> f : fileMap.entrySet()) {
MultipartFile file = f.getValue();
if (!isLegalFile(file)) {
String msg = "is a illegal file";
throw new RuntimeException(msg);
}
String originalFileName = prefix + "_" + file.getOriginalFilename();
File fileDir = new File(path + "/upload" + File.separator);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File files = new File(path + "/upload" + File.separator
+ originalFileName);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(files);
fileOutputStream.write(file.getBytes());
fileOutputStream.flush();
attachIds.append(originalFileName + ",");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
writer.write(attachIds.toString().substring(0,attachIds.toString().length()-1));
}
private final String[] fileType = new String[]{".dat",".264",".h264",".mp4",".dav",".MP4",".AVI",".ts",".avi",".mpg",".rmvb",".flv",".rm",".mov",".wmv",
".JPG",".bmp",".png",".BMP",".jpg",".PNG",".gif",
".xlsx",".xls",".txt",".pdf",".doc",".docx",
".rar",".zip",".7z"};
private boolean isLegalFile(MultipartFile file) {
String originalFileName = file.getOriginalFilename();
for(String ft : fileType) {
if (originalFileName.endsWith(ft)) {
return true;
}
}
return false;
}
}
效果图:


猜你喜欢
- Mybatis事务管理我们可以在mybatis-config.xml中配置事务管理器的实现<transactionManager ty
- 建库建表DROP DATABASE IF EXISTS mp;CREATE DATABASE mp DEFAULT CHARACTER SE
- java继承1.Object类的常用方法方 * 能public Boolean equals(Object obj)判断两个对象变量所指向的是
- 1.docker安装seata 1.3.0镜像docker pull seataio/seata-server:1.3.02.运行容器获取配
- 本文实例讲述了C#判断多个文本框是否为空的方法。分享给大家供大家参考。具体实现方法如下:/// <summary>/// 自定义
- 我就废话不多说了,还是上代码吧接口:interface OnBind {fun onBindChildViewData(holder: St
- 前言众所周知,微信聊天中我们输入一些关键词会有表情雨下落,比如输入「生日快乐」「么么哒」会有相应的蛋糕、亲吻的表情雨下落,今天就来完成这个表
- 先看看电影票在线选座功能实现的效果图:界面比较粗糙,主要看原理。这个界面主要包括以下几部分1、座位 2、左边的排数 3、左上方的缩略图 4、
- RecyclerView的使用比ListView的使用是比较复杂的,ListView的使用是五个步骤,而我们的RecyclerView的使用
- 前言:学习了SpringBoot分页查询的两种写法,一种是手动实现,另一种是使用框架实现。现在我将具体的实现流程分享一下。首先是手动实现分页
- 1.内部类概念及分类将一个类定义在另一个类的内部或者接口内部或者方法体内部,这个类就被称为内部类,我们不妨将内部类所在的类称为外围类,除了定
- JAVA中实现pdf转图片可以通过第三方提供的架包,这里介绍几种常用的,可以根据自身需求选择使用。一、icepdf。有收费版和开源版,几种方
- 写在前面关于数据结构,Java官方其实已经帮我们写好并封装起来了,在真正需要使用的时候直接调用即可,但为了更好的理解数据结构,我会按照源码的
- 本文主要讲解安装AndroidStudio和配置环境变量遇到一些问题,以及解决方法。需要的软件:AndriodStudio安装包.java
- 1. 什么是单例模式单例模式指的是在应用整个生命周期内只能存在一个实例。单例模式是一种被广泛使用的设计模式。他有很多好处,能够避免实例对象的
- ES是一个基于Lucene的分布式全文搜索服务器,和SQL Server的全文索引(Fulltext Index)有点类似,都是基于分词和分
- 1.Quartz是什么?Quartz是一个开源的Java调度框架,可以用来实现在指定的时间或时间间隔触发任务执行的功能。它支持多种方式的作业
- 前言Java.util包中的List接口继承了Collection接口,用来存放对象集合,所以对这些对象进行排序的时候,要么让对象类自己实现
- 本文实例讲述了C#实现Base64处理的加密解密,编码解码。分享给大家供大家参考,具体如下:using System;using Syste
- 相信对于手机的时间日期设置大家一定都不陌生吧,今天举一个关于时间日期设置的示例,其中有些许不完善之处,例如如何使设置的时间日期和手机系统同步