1、 下載下傳到官方網址下載下傳“uploadify3.2”插件。
2、 下載下傳swfobject_2_2js插件
3、 搭建struts2架構。
4、 建立upload2.jsp頁面,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" target="_blank" rel="external nofollow" >
<title>Jquery插件上傳</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="js/uploadify/uploadify.css" target="_blank" rel="external nofollow" >
<script type="text/javascript" src="js/uploadify/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/uploadify/swfobject.js"></script>
<script type="text/javascript" src="js/uploadify/uploadify.js"></script>
<script type="text/javascript" src="js/uploadify/uploadify.min.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css" target="_blank" rel="external nofollow" >
-->
<script type="text/javascript">
$(function() {
$("#file_upload").uploadify({
debug:false,
auto:true,//是否自動上傳
height: 30,
buttonText:'上傳檔案',
cancelImage:'js/uploadify/uploadify-cancel.png',
swf : 'js/uploadify/uploadify.swf',
expressInstall:'js/uploadify/expressInstall.swf',
uploader : '<%=path%>/upload/fileUpload.action', //背景處理上傳檔案的action
width : 120 ,
multi:false,//是否允許多個檔案上傳
queueID:'uploadfileQueue',
fileObjName:'fileName', //與背景Action中file屬性一樣
formData:{ //附帶值
'userid':'111',
'username':'tom',
'rnd':'111'
},
successTimeout:99999,//上傳逾時時間
overrideEvents:['onDialogClose'],
fileTypeDesc:'上傳檔案支援的檔案格式:jpg,jpge,gif,png',
fileTypeExts:'*.*',/
package com.ys.uploadfile.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport implements ServletRequestAware {
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private List<File> fileName;//這裡的"fileName"一定要與表單中的檔案域名相同
private List<String> fileNameContentType;//格式同上"fileName"+ContentType
private List<String> fileNameFileName;//格式同上"fileName"+FileName
private String savePath;//檔案上傳後儲存的路徑
public void upload() throws Exception {//intentionPicture
String uploadFileName="";
File dir=new File(getSavePath());
String savePath=getSavePath();//儲存上傳檔案的位址
if(!dir.exists()){
dir.mkdirs();
}
List<File> files=getFileName();
for(int i=0;i<files.size();i++){
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getFileNameFileName().get(i));
FileInputStream fis=new FileInputStream(getFileName().get(i));
byte []buffers=new byte[1024];
int len=0;
while((len=fis.read(buffers))!=-1){
fos.write(buffers,0,len);
}
fos.close();
fis.close();
uploadFileName=getFileNameFileName().get(i);
}
//設定響應内容的字元串編碼
ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
ServletActionContext.getResponse().setContentType("text/plain");
ServletActionContext.getResponse().getWriter().print(uploadFileName+","+savePath+"\\"+uploadFileName);
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return "success";
}
public InputStream getInputStream() {
return ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
}
public void setServletRequest(HttpServletRequest req) {
this.request=req;
}
public List<File> getFileName() {
return fileName;
}
public void setFileName(List<File> fileName) {
this.fileName = fileName;
}
public List<String> getFileNameContentType() {
return fileNameContentType;
}
public void setFileNameContentType(List<String> fileNameContentType) {
this.fileNameContentType = fileNameContentType;
}
public List<String> getFileNameFileName() {
return fileNameFileName;
}
public void setFileNameFileName(List<String> fileNameFileName) {
this.fileNameFileName = fileNameFileName;
}
@SuppressWarnings("deprecation")
public String getSavePath() {
return request.getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
6、 struts2.xml配置内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 上傳檔案的action -->
<constant name="struts.multipart.maxSize" value="1024000000"/>
<package name="upload" namespace="/upload" extends="struts-default">
<action name="fileUpload" class="com.ys.uploadfile.action.FileUploadAction" method="upload">
<param name="savePath">/uploads</param>
</action>
</package>
</struts>
注:uploadify的屬性配置請參考附件