uploadify java 下载_uploadify java实现多文件上传和预览

本文介绍了如何使用Uploadify插件在Java应用中实现实时文件上传,并通过Spring MVC控制器处理上传文件,同时展示了合法文件类型的检查和预览。适合学习文件上传和Spring框架结合的开发者。
摘要由CSDN通过智能技术生成

本文实例为大家分享了java文件上传和预览实现代码,供大家参考,具体内容如下

1、下载uploadify插件

cd98999f7451b2fd94b8683ae0dabe61.png

2、index.html

#uploader {

position: relative;

}

#uploader_queue {

position: absolute;

width: 600px;

left: 200px;

top: 0;

}

$(function() {

$("#file_upload")

.uploadify(

{

'auto' : false,

'method' : "get",

'formdata' : {

'folder' : 'file'

},

'height' : 30,

'swf' : '/js/uploadify-v3.1/uploadify.swf', // flash

'uploader' : '/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(

'
');

},

'onqueuecomplete' : function(queuedata) {

$('#uploader_msg').html(

queuedata.uploadssuccessful

+ '个文件上传成功
');

}

});

});

上传

取消上传

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 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 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;

}

}

效果图:

85365fb46dfab2e994451a7ae5daae8b.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值