多照片上传插件代码Word格式.docx
《多照片上传插件代码Word格式.docx》由会员分享,可在线阅读,更多相关《多照片上传插件代码Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
![多照片上传插件代码Word格式.docx](https://file1.bdocx.com/fileroot1/2023-1/27/b58ff4e8-08bf-455b-984d-ba039ced031e/b58ff4e8-08bf-455b-984d-ba039ced031e1.gif)
publicvoidinit()throwsServletException{
//获取本项目所在真实硬盘目录
Stringpath=getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath();
path=path.substring(0,path.indexOf("
WEB-INF"
));
//判断目标是否存在,不存在就建立
StringuploadDir=path.concat(this.getInitParameter("
uploadDir"
System.out.println("
uploadDir:
="
+uploadDir);
StringtempUploadDir=path.concat(this.getInitParameter("
tempUploadDir"
tempUploadDir:
+tempUploadDir);
Filef_uploadDir=newFile(uploadDir);
Filef_tempUploadDir=newFile(tempUploadDir);
if(!
f_uploadDir.exists()){
f_uploadDir.mkdirs();
}
f_tempUploadDir.exists()){
f_tempUploadDir.mkdirs();
//给变量赋值
this.uploadDir=uploadDir;
this.tempUploadDir=tempUploadDir;
}
*@see
*javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
*,javax.servlet.http.HttpServletResponse)不接收get方式提交的数据,返回上传失败状态码。
protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
this.setResponse(response);
PrintWriterout=response.getWriter();
out.print("
{\"
error\"
:
\"
-1\"
"
);
//非法提交方式
*javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
*,javax.servlet.http.HttpServletResponse)上传文件请求都是通常POST提交
protectedvoiddoPost(HttpServletRequestrequest,
//设置响应类型,以便前端解析
Stringresult="
try{
//检查本次是否一个文件上传请求
booleanisMultipart=ServletFileUpload.isMultipartContent(request);
if(isMultipart){
DiskFileItemFactoryfactory=newDiskFileItemFactory();
//创建一个工厂基于磁盘的文件项
factory.setRepository(newFile(tempUploadDir));
//配置储存库(确保安全的临时位置时)
ServletFileUploadupload=newServletFileUpload(factory);
//创建一个新的文件上传处理程序
upload.setSizeMax(1024*1024*100);
//设置总体要求尺寸限制(建议前后台分别设置,因为前后台用到了不同的插件)
List<
FileItem>
items=upload.parseRequest(request);
//解析请求
Iterator<
iter=items.iterator();
//处理上传的项目
while(iter.hasNext()){//如果是一次性上传多个文件,那这里会分别去保存
FileItemitem=iter.next();
if(!
item.isFormField()){//过滤表单里的非文件类型字段
if(!
.equals(item.getName())){//过滤非文件类型的input
Strings_name=item.getName();
//获得原始文件名
intposition=s_name.lastIndexOf("
."
Strings_fileType=s_name.substring(position,
s_name.length());
//获得文件后缀
Stringdate=newSimpleDateFormat("
yyyyMMdd"
)
.format(newDate());
Strings=uploadDir.concat("
).concat(date)
.concat("
//这里按日期分目录保存文件
Filesf=newFile(s);
if(!
sf.exists()){
sf.mkdirs();
}
Strings_filePath=s.concat(
UUID.randomUUID().toString()).concat(
s_fileType);
Filepath=newFile(s_filePath);
item.write(path);
result+=s_filePath.concat("
"
}else{
result="
break;
}
}
}
}else{
result="
}
Strings_resultJSON=this.jointJSON(result);
//拼接返回前端JSON
out.print(s_resultJSON);
}catch(Exceptione){
e.printStackTrace();
}finally{
out.flush();
out.close();
/**
*拼接JSON,将保存文件的文件名和日期目录返回给前端(前端可能需要这个路径完成其他表单操作,比如将文件路径存放到数据库)
*@paramresult
*JSON格式的字符串
*@return
*@throwsUnsupportedEncodingException
privateStringjointJSON(Stringresult)throwsUnsupportedEncodingException{
Stringstr="
.equals(result)){
Stringrs[]=result.split("
StringBufferbuffer=newStringBuffer("
rows\"
["
for(inti=0;
i<
rs.length;
i++){
Strings_tmpName=rs[i];
s_tmpName=s_tmpName.substring(uploadDir.length(),s_tmpName
.length());
buffer.append("
name\"
).append(s_tmpName).append("
},"
str=buffer.toString();
str=str.substring(0,str.length()-1).concat("
]}"
}else{
str="
-2\"
//上传失败
returnstr;
*设置响应类型ContentType为"
application/x-json"
*@paramresponse
privatevoidsetResponse(HttpServletResponseresponse){
response.setCharacterEncoding("
UTF-8"
response.setContentType("
application/json;
charset=UTF-8"
response.setHeader("
cache-control"
"
no-cache"
}
Web.xml-------配置
<
?
xmlversion="
1.0"
encoding="
>
web-appxmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
xmlns="
xsi:
schemaLocation="
id="
WebApp_ID"
version="
2.5"
<
display-name>
sd<
/display-name>
welcome-file-list>
welcome-file>
test_upload.html<
/welcome-file>
/welcome-file-list>
servlet>
description>
专门用来处理上传操作的servlet<
/description>
servlet-name>
FileUploadServlet<
/servlet-name>
servlet-class>
com.xiaoxing.upload.FileUploadServlet<
/servlet-class>
init-param>
文件存放的正式目录,可以自己配置<
param-name>
uploadDir<
/param-name>
param-value>
/upload/images/<
/param-value>
/init-param>
文件存放的临时目录,可以自己配置,里的文件由下面配置的监听器自动删除。
tempUploadDir<
/upload/temp<
/servlet>
<
DeleteServlet<
com.xiaoxing.upload.DeleteServlet<
servlet-mapping>
url-pattern>
/delImg<
/url-pattern>
/servlet-mapping>
/upload<
listener>
临时文件资源清理,工具包自带,不用我们来写<
listener-class>
mons.fileupload.servlet.FileCleanerCleanup<
/listener-class>
/listener>
/web-app>
test_upload.html-------前台页面
!
DOCTYPEhtml>
html>
head>
metacharset="
GBK"
title>
jQueryUploadify+ApacheFileupload异步上传文件示例(2014-5-3)<
/title>
linkrel="
stylesheet"
type="
text/css"
href="
uploadify.css"
scriptsrc="
jquery-1.9.0.min.js"
/script>
jquery.uploadify.min.js"
scripttype="
text/javascript"
$(function(){
$('
#fileupload'
).uploadify({
'
method'
:
'
post'
buttonText'
flash上传文件'
fileSizeLimit'
2048KB'
fileTypeExts'
*.gif;
*.jpg;
*.png'
swf'
uploadify.swf'
uploader'
upload'
//这是上传图片的路径,也就是我在web.xml里配置的servlet
onUploadSuccess'
function(file,data,response){//图片上传成功后返回数据在这里处理
varary=eval("
("
+data+"
)"
).rows;
vardata=$("
#data"
).val()+1;
$("
).val(data);
for(vari=0;
ary.length;
$("
#J_div"
).append(
"
divstyle='
position:
relative;
width:
160px;
height:
200px;
float:
left;
margin-right:
10px;
'
divclass='
close'
style='
absolute;
top:
8px;
right:
cursor:
pointer;
imgsrc='
window-close.png'
/>
/div>
imgid='
acc"
+data.length+"
alt='
图片'
src='
upload/images"
+ary[i].name
+"
width='
160px'
height='
200px'
div>
inputtype='
hidden'
name='
imges"
value='
.close"
).click(function(){
varimg=$(this).parent().next().children().val();
$.post(
delImg"
{method:
img:
img},
"
xml"
$(this).parent().next().remove();
$(this).parent().remove();
});
});
});
styletype="
.img_boxx{position:
135px;
display:
inline;
padding:
05px;
margin-bottom:
.img_boxxspan{background:
#fff;
position:
0px;
5px;
15px;
block;
.img_remove{cursor:
pointer;
/style>
/head>
body>
formaction="
sss.jsp"
<
inputid="
data"
name="
value="
fileupload"
file"
img"
multiple="
multiple"
/>
divid="
J_div"
--<
img_boxx'
inputtype="
submit"
提交"
-->
/form>
/body>
/html>
Uploadify.css---------样式
/*
Uploadify
Copyright(c)2012ReactiveApps,RonnieGarcia
ReleasedundertheMITLicense<
//www.opensource.org/licenses/mit-license.php>
*/
.uploadify{
relative;
1em;
.uploadify-button{
background-color:
#50