手動で作成したアップロードドキュメントの進捗バー
10214 ワード
JavaBean(DWRとフロントパスのためのもの)
public class UploadInfo {
private List<Long> hasUpload = new ArrayList<Long>();
private List<Long> totalSize = new ArrayList<Long>();
getter and setter....
}
アップロードのアクションを処理する @Override
public String execute() throws Exception {
DocumentCatalog catalog = catalogServie.getDocumentCatalog(catalogId);
List<DocumentItem> list = new ArrayList<DocumentItem>();
DocumentItem item = null;
Date date = new Date();
String type;
String fileName;
File f = null;
// long totalSize = 0;
// for(File ss : file){
// totalSize += ss.length();
// }
//
// ,
UploadInfo uploadInfo = new UploadInfo();
for (int i = 0; i < file.size(); ++i) {
item = new DocumentItem();
fileName = fileFileName.get(i);
f = file.get(i);
item.setCatalog(catalog);
item.setCreateDate(date);
item.setRealSize(f.length());
item.setName(fileName);
type = FileUtils.getFileExtName(fileName, false);
item.setType(type);
item.setSize(FileUtils.getStringSize(f.length()));
item.setRandomName(FileUtils.getRandomFileName(5)+"."+type);
String separator = File.separator;
//
String itemStorage = FileStorage.getDocumentItemStoragepath(ServletActionContext.getRequest());
//
File upload = new File(itemStorage + separator + catalogId + separator + item.getRandomName());
// , , JAVA ,
org.apache.commons.io.FileUtils.forceMkdir(upload.getParentFile());
//uploadInfo.setTotalSize(totalSize);
uploadInfo.getTotalSize().add(f.length());
// , hasUpload !
uploadInfo.getHasUpload().add(0L);
ServletActionContext.getRequest().getSession().setAttribute("uploadInfo", uploadInfo);
InputStream is = new FileInputStream(f);
byte[] buffer = new byte[1024];
int len = 0;
OutputStream os = new FileOutputStream(upload);
while (-1 != (len = is.read(buffer))) {
//Thread.sleep(10);
os.write(buffer, 0, len);
//
uploadInfo.getHasUpload().set(i, uploadInfo.getHasUpload().get(i)+len);
}
is.close();
os.close();
//
list.add(item);
}
ServletActionContext.getRequest().getSession().removeAttribute("uploadInfo");
this.service.addDocumentItems(catalog, list);
return SUCCESS;
}
JSPコード<tr>
<td colspan="2">
<div id="bar" style="display:none;">
</div>
</td>
</tr>
JSコード:<script type="text/javascript">
var count=0; //
var num=1;
var maxUploadFileCount = 4; //
function addMore(){
if(count == maxUploadFileCount){
alert(" "+(maxUploadFileCount+1)+" ");
return ;
}
var theTd = document.getElementById("td");
var br = document.createElement("br");
var obj = document.createElement("input");
var button = document.createElement("input");
obj.type = "file";
obj.name = "file";
obj.id = "file"+ ++num;
obj.size = 40;
button.type = "button";
button.value = " ";
button.onclick = function(){
theTd.removeChild(button);
theTd.removeChild(obj);
theTd.removeChild(br);
--count;
};
theTd.appendChild(br);
theTd.appendChild(obj);
theTd.appendChild(button);
++count;
}
function validate(){
var theTr = document.getElementById("tr");
var firstFile = document.getElementById("file") ;
if(firstFile!=null && firstFile.value.length==0){
alert(" !");
return false;
}
for(var i=0;i<num;++i){
var file = document.getElementById("file"+num);
if(file!=null && file.value.length==0){
alert(" !");
return false;
}
}
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;++i){
var type = inputs[i].type;
if( type=="button" || type=="submit" || type=="reset"){
inputs[i].disabled = true;
}
if(type=="file"){
inputs[i].readOnly=true;
}
}
startUpload();
return true;
}
function startUpload(){
var outerDiv = document.getElementById("bar");
var fileArray = new Array();
var inputs = document.getElementsByTagName("input");
for(var i=0;i<inputs.length;++i){
if(inputs[i].type == "file"){
var fileName = inputs[i].value;
var index = fileName.indexOf("\\");
if(-1 == index){
index = fileName.indexOf("/");
}
fileName = fileName.substring(index+1,fileName.length);
fileArray.push(fileName);
}
}
for(var i=0;i<count+1;++i){
var numDiv = document.createElement("div");
var fileName = fileArray[i];
if(fileName.length>35){
fileName = fileName.substring(0,35)+"...";
}
fileName +=" "
numDiv.id = "num"+i;
numDiv.innerHTML = i+1+": ";
numDiv.style.cssFloat = "left"; // FF
numDiv.style.styleFloat = "left"; // IE
var nameDiv = document.createElement("div");
nameDiv.id = "name"+i+1;
nameDiv.style.cssFloat = "left"; // FF
nameDiv.style.styleFloat = "left"; // IE
nameDiv.style.width="320px";
nameDiv.innerHTML = fileName;
// <div id="bar" style="width: 300px; height: 20px; border: 1px; background: #eee; float: left;">
// <div id="processBar" style="width: 0px; height: 20px; background: green;"></div>
// </div>
var innerDiv = document.createElement("div");
innerDiv.style.width = "300px";
innerDiv.style.height = "20px";
innerDiv.style.border = "1px";
innerDiv.style.background = "#eee";
innerDiv.style.cssFloat = "left"; // FF
innerDiv.style.styleFloat = "left"; // IE
var processBarDiv = document.createElement("div");
processBarDiv.id = "processBar"+i;
processBarDiv.style.width = "0px";
processBarDiv.style.height = "20px";
processBarDiv.style.background = "green";
// processBarDiv.style.cssFloat = "left"; // FF
// processBarDiv.style.styleFloat = "left"; // IE
var rateDiv = document.createElement("div");
rateDiv.id = "rateDiv"+i;
rateDiv.innerHTML = " "+"0%";
outerDiv.appendChild(numDiv);
outerDiv.appendChild(nameDiv);
innerDiv.appendChild(processBarDiv);
outerDiv.appendChild(innerDiv);
outerDiv.appendChild(rateDiv);
}
window.setTimeout("startProcess()",200);
}
function startProcess(){
uploadInfoListener.getUploadInfo(showProcessBar);
}
function showProcessBar(data){
var process = 0;
var barBox = document.getElementById("bar");
barBox.style.display = "block";
var test = document.getElementById("test");
for(var i=0;i<count+1;++i){
process=data.hasUpload[i]/data.totalSize[i]*100;
if(isNaN(process)){
if(data.hasUpload[i]>0){
process=100;
}else{
process=0;
}
}
var processRate = document.getElementById("rateDiv"+i);
processRate.innerHTML = Math.floor(process)+"%";
var processBar = document.getElementById("processBar"+i);
processBar.style.width= process*3+"px";
test.innerHTML="hasUpload="+data.hasUpload[i]+"<br>totalSize="+data.totalSize[i]+"<br>Process="+process;
}
window.setTimeout("startProcess()",200);
}
</script>