本文实例为大家分享了java文件上传和预览实现代码,供大家参考,具体内容如下
1、下载uploadify插件
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;
}
}
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!