文件上传 进度条 一次一个

[/code]jsp

[code="java"]
<%@ page import="com.huadi.system.datas.SystemResource" %>
<%--
Created by IntelliJ IDEA.
User: 王涛
Date: 2006-9-6
Time: 11:08:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=gb2312" language="java" %>
<%
String resultid = request.getParameter("resultid");
String savename = request.getParameter("savename");
String displayname = request.getParameter("displayname");

%>
<html>
<head><title>文件上传页面</title>
<script language="javascript" src="<%=SystemResource.DOMAIN%>/admin/js/commonajax.js"></script>

<script language="javascript">
var savevalue = opener.document.getElementById('<%=savename%>').value;

var ooflag = "0";

function initparams(_displayvalue) {
if (_displayvalue.length < 2) {
document.getElementById("filelist").innerHTML = "";
}
var _disvalue = _displayvalue.split(";");
var filelist = "";
for (var i = 0; i < _disvalue.length; i++) {
if (_disvalue[i] == "")
continue;
var data = _disvalue[i].split("=");

filelist = filelist + "<li>" + data[0] + "(" + data[1] + "M)    <a href=\"javascript:deleteFile('" + data[0] + "')\">删除</a></li>";
if (i < _disvalue.length - 1)
filelist = filelist + "<br/>";

}
document.getElementById("filelist").innerHTML = filelist;
}

function initoper(name, disname) {
var lname = name.replace(/#/g, "%23");
return "<a href='<%=SystemResource.DOMAIN%>/actions/common/open-file.action?id=<%=resultid%>&name=" + lname + "'>" + name + "(" + disname + "M)</a>";

}

function checkUpload() {
if (form1.file.value == "") {
alert("您还没有选择要上传的附件!");
return false;
}
return true;
}
function goupload(ff) {
if (checkUpload()) {
ooflag = ff;
form1.action = "<%=SystemResource.DOMAIN%>/actions/common/upload.action?_result_id=<%=resultid%>";
form1.submit();
go();
}
}


function go() {
createXmlHttp();
clearBar();
var ts = "ts=" + new Date().getTime();
var url = "<%=SystemResource.DOMAIN%>/actions/common/progress.action?ts=" + ts;
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = pollCallback;
xmlHttp.send(null);
}


function pollCallback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var percent_complete = xmlHttp.responseText.substring(6);

if (percent_complete.indexOf("error") != -1) {
alert(percent_complete.substring(6));
statusreset();
return;
}

var progress = document.getElementById("progress");
var progressPersent = document.getElementById("progressPersent");
if (percent_complete.indexOf("percent") == 0) {
var pc = percent_complete.substring(8);
progress.style.width = pc + "%";
progressPersent.innerHTML = pc + "%";
setTimeout("go()", 300);
} else {
progress.style.width = "100%";
progressPersent.innerHTML = "100%";
document.getElementById("complete").innerHTML = "文件上传完毕!";
alert("上传成功!");
statusreset();

if (savevalue.length > 5) {
savevalue += ";";
}
savevalue += percent_complete;
//截取字符串
var fileName1 = savevalue.indexOf("=");
savevalue = savevalue.substring(0,fileName1);
savevalue=savevalue;
opener.document.getElementById('<%=savename%>').value = savevalue;
opener.document.getElementById('<%=displayname%>').innerHTML = getDisvalue(savevalue);
if (ooflag == "0") {
window.close();
} else {
initparams(savevalue);
}
}

}
}
}

function getDisvalue(value) {
if (value.length < 2) {
return "";
}
var names = value.split(";");
var disvalues = "";
for (var i = 0; i < names.length; i++) {
var name_size = names[i].split("=");

disvalues = disvalues + initoper(name_size[0], name_size[1]);
if (i < names.length - 1) {
disvalues += "<br>";
}
}
return disvalues;
}

function statusreset() {
document.getElementById("filepath").innerHTML = "<input type='file' name='file' size=\"60\" >";
document.getElementById("upload").style.visibility = "visible";
document.getElementById("progressBar").style.visibility = "hidden";
progress.style.width = "1%";
progressPersent.innerHTML = "1%";
}

function deleteFile(filename) {

deledfile = filename;
var query = "<%=SystemResource.DOMAIN%>/actions/common/del-file.action?id=<%=resultid%>&name=" + filename ;
createGetRequest(query, true);
}

function getFileName(name) {
return name.substring(0, name.lastIndexOf("("));
}

function doAction() {
var rrmm = xmlHttp.responseText.substring(6);
if (rrmm.indexOf("error") == -1) {
var names = savevalue.split(";");
savevalue = "";
for (var i = 0; i < names.length; i++) {
if (names[i].indexOf(rrmm) == 0) {
continue;
}
if (savevalue.length > 2) {
savevalue += ";";
}

savevalue += names[i];
}

initparams(savevalue);
opener.document.getElementById('<%=savename%>').value = savevalue;
opener.document.getElementById('<%=displayname%>').innerHTML = getDisvalue(savevalue);
alert("删除成功!");

} else {
alert("删除失败!");
}


}
function clearBar() {
var progress_bar = document.getElementById("progressBar");
var progressPersent = document.getElementById("progressPersent");
document.getElementById("upload").style.visibility = "hidden";
var complete = document.getElementById("complete");
progress_bar.style.visibility = "visible";
progressPersent.innerHTML = " ";
complete.innerHTML = "文件上传中........";
}
</script>

<link href="<%=SystemResource.DOMAIN%>/admin/css/pingtai.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="<%=SystemResource.DOMAIN+"/css/style.css"%>">

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
body {
background-color: #FFFFFF;
}

#filelist li {
line-height: 20px;
border-bottom: #154BA0 dashed 1px;
background: #FFFFFF
}

.style2 {
color: #FF0000
}

-->
</style>
</head>

<body class="bodycolor" style="margin-left:0px;margin-top:0px;margin-right:0px;margin-bottom:0px">
<table width="100%" border="0" align="center" cellPadding="3" cellSpacing="0" class="TableList">
<tr>
<td align="left" class="TableHeader2">您现在的位置:文件上传页面</td>
</tr>

</table>

<div id="upload">
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1" target="process">
<table style="width:100%" align="center" class="TableBlock">
<tr>
<td colspan="2" nowrap align="left" class="TableData">已经上传附件列表>></td>
</tr>
<tr>
<td colspan="2" class="TableData">
<div id="filelist"></div>
 
</td>
</tr>
<tr>
<td class="TableData" align="right" width="40%">选择文件:</td>
<td class="TableData">
<div id="filepath"><input name="file" type="file" size="60"/>
</div>
</td>
</tr>
</table>
<table style="width:100%" border="0" align="center" cellspacing="0" cellpadding="0" class="TableBlock">
<tr align="center" class="TableControl">
<td align="center" class="TableData" colSpan="5">
<INPUT class="SmallButton" type="button" value="确 定" onClick="goupload('0')">
  
<INPUT class="SmallButton" type="button" value="确定并附加另一个" onClick="goupload('1')">

</td>
</tr>
</table>


</form>
</div>

<div id="progressBar" style="padding:0px;border:solid black 0px;visibility:hidden">
<table width="360" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center" class="style2" id="progressPersent">1%</td>
</tr>
<tr>
<td>
<table width="100%" cellpadding="0" cellspacing="0"
style=" border-bottom: #333333 solid 1px; border-top:#333333 solid 1px; border-left:#333333 solid 1px; border-right:#333333 solid 1px">
<tr>
<td>
<table width="1%" border="0" cellspacing="0" cellpadding="0" bgcolor="#3980F4"
id="progress">
<tr>
<td> </td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="30" align="center" id="complete">completed</td>
</tr>
</table>
<script language="javascript">
initparams(savevalue);

</script>
</div>
<iframe name="process" width="0" height="0"/>
</body>

</html>





package com.huadi.system.frame.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huadi.system.beans.FileUploadBean;
import com.huadi.system.beans.upload.ProgressBar;
import com.huadi.system.datas.SystemResource;
import com.huadi.system.exception.FrameException;
import com.huadi.system.mediadata.MediaData;
import com.huadi.system.mediadata.UploadMediaData;
import com.huadi.system.mediadata.model.UploadModel;

/**
* Created by IntelliJ IDEA.
*
* To change this template use File | Settings | File Templates.
*/
public class FileUploadAction extends ActionSupport {
private UploadMediaData model = new UploadMediaData();

public String execute(HttpServletRequest request,
HttpServletResponse response) throws FrameException {
UploadModel um = model.getModel();
String dir = um.getDir();
String types = um.getTypes();
String maxSize = um.getMaxSize();
long size = 0;

// 判断是否是绝对路径
if (dir.indexOf(":/") == -1) {
dir = SystemResource.CONFIG_FOLDER.substring(0,
SystemResource.CONFIG_FOLDER.lastIndexOf("\\WEB-INF\\"))
+ dir;
}

try {
size = Long.parseLong(maxSize) * 1024 * 1024;
} catch (NumberFormatException ex) {
size = 10 * 1024 * 1024;
}

FileUploadBean upload = new FileUploadBean(dir, request);
if (types.trim().length() > 0) {
String[] ext = types.split(",");
for (String s : ext) {
upload.addTyps(s);
}
}
upload.setMaxSize(size);
ProgressBar bar = ProgressBar.getBar(request);

try {
upload.upload();
} catch (Exception ex) {
synchronized (bar) {
bar.setFinished(true);
bar.setInfos("error=" + ex.getMessage());
}
System.out.println("dd");
return "dd";
}

String[] names = upload.getFileNames();
String[] sizes = upload.getFileSizes();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < names.length - 1; i++) {
sb.append(names[i]);
sb.append("=");
sb.append(sizes[i]);
sb.append(";");
}
int index = names.length;
if (index > 0) {
sb.append(names[index - 1]);
sb.append("=");
sb.append(sizes[index - 1]);
}
synchronized (bar) {
bar.setFinished(true);
bar.setInfos(sb.toString());
}
System.out.println("default");
return "default";
}

public MediaData getMediaData() {
return model;
}
}





package com.huadi.system.beans;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.huadi.system.beans.upload.ProgressBar;
import com.huadi.system.beans.upload.naming.FileNaming;
import com.huadi.system.beans.upload.naming.NamingFactory;
import com.huadi.system.frame.exception.UploadErrorException;

/**
* Created by IntelliJ IDEA. User: 王涛 Date: 2008-1-12 Time: 14:40:37 To change
* this template use File | Settings | File Templates.
*/
public class FileUploadBean {
private String saveDir;
private String charset;
private long maxSize = 1024 * 1024 * 10;
private HttpServletRequest request;
private Map<String, List<String>> paramList = new HashMap<String, List<String>>();

private Set<String> fileTypes;
private String queryString;
private String boundary = "";
private String[] fileNames;
private String[] fileSizes;
private int len = 0;

private String userId = "";
private String userName = "";

private String nameScheam = "default";

private ProgressBar bar;

public void setNameScheam(String nameScheam) {
this.nameScheam = nameScheam;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getCharset() {
return charset;
}

public void setCharset(String charset) {
this.charset = charset;
}

public long getMaxSize() {
return maxSize;
}

public void setMaxSize(long maxSize) {
this.maxSize = maxSize;
}

public String getParameter(String name) {
List<String> params = paramList.get(name);
if (params == null || params.size() == 0) {
return null;
}
return params.get(0);
}

public String[] getParameterValues(String name) {
List<String> params = paramList.get(name);
if (params == null || params.size() == 0) {
return null;
}

String[] value = new String[params.size()];
return params.toArray(value);

}

public String getQueryString() {
return queryString;
}

public String[] getFileNames() {
return fileNames;
}

public FileUploadBean(String saveDir, HttpServletRequest request) {
this.saveDir = saveDir;
this.request = request;
File file = new File(this.saveDir);
if (!file.exists()) {
file.mkdirs();
}
boundary = this.request.getContentType().substring(30); // 得到内存中数据分界符
queryString = this.request.getQueryString();
bar = ProgressBar.getBar(request);
int size = request.getContentLength();

bar.setTotal(size);
}

public void addTyps(String type) {
if (fileTypes == null) {
fileTypes = new HashSet<String>();
}
fileTypes.add(type);
}

public void upload() throws ServletException, IOException,
UploadErrorException {
setCharset(request.getCharacterEncoding());
if (bar.getTotal() > maxSize)
throw new IOException("文件已超过最大值:" + maxSize / (1024 * 1024) + "M");

if (!uploadFile(request.getInputStream())) {
throw new UploadErrorException();
}
}

private boolean uploadFile(ServletInputStream servletinputstream)
throws IOException, UploadErrorException {
String line;
byte[] buffer = new byte[256];
List<String> _size = new ArrayList<String>();
byte[] lastbuffer;
int lastlen;
List<String> tmpFileName = new ArrayList<String>();
long size = 0L;
while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.startsWith("Content-Disposition: form-data;")) {
int i = line.indexOf("filename=");

if (i >= 0) {

String fName = getFileName(line);

if (fName.equals("")) {
continue;
}
if (fName.indexOf("\"") != -1) {
fName = fName.replaceAll("\"", "");
}
String ext = fName.substring((fName.lastIndexOf(".") + 1));

if ((fileTypes != null) && !fileTypes.contains(ext)) {
throw new UploadErrorException("上传文件的类型不允许!!");
}
String fn = fName.substring(0, fName.lastIndexOf("."));

tmpFileName.add(fName);
FileNaming scheam = NamingFactory.getNameScheam(nameScheam);
fName = scheam.getNextName(saveDir, fn, ext);

while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.length() <= 2) {
break;
}
}

File f = new File(fName);
FileOutputStream dos = new FileOutputStream(f);

lastbuffer = new byte[0];
lastlen = 0;

while ((line = readLine(buffer, servletinputstream, null)) != null) {
size += len;
bar.setNowaData(size);

if (line.indexOf(boundary) != -1) {
if (lastlen > 2) {
dos.write(lastbuffer, 0, lastlen - 2);
}

break;
}

dos.write(lastbuffer, 0, lastlen);
lastbuffer = arraycopy(buffer, 0, len);
lastlen = len;
}

BigDecimal bg = new BigDecimal((double) size
/ (1024 * 1024)).setScale(2,
BigDecimal.ROUND_HALF_UP);
scheam.saveInfos(userId, userName);
_size.add(bg.floatValue() + "");
dos.close();
} else {

String key = getKey(line);
String value = "";

while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.length() <= 2) {
break;
}
}

while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.indexOf(boundary) != -1) {
break;
}

value += line;
}

put(key, value.trim());
}
}
}

if (queryString != null) {
String[] each = queryString.split("&");
for (String s : each) {
String[] nv = s.split("=");
if (nv.length == 2) {
put(nv[0], nv[1]);
}
}
}

fileNames = new String[tmpFileName.size()];
fileNames = tmpFileName.toArray(fileNames);

fileSizes = new String[_size.size()];
fileSizes = _size.toArray(fileSizes);

return fileNames.length != 0;
}

/**
* 获取上传文件的大小
*
* @return
*/
public String[] getFileSizes() {
return fileSizes;
}

private String readLine(byte[] Linebyte,
ServletInputStream servletinputstream, String charset) {
try {
len = servletinputstream.readLine(Linebyte, 0, Linebyte.length);

if (len == -1) {
return null;
}

if (charset == null) {
return new String(Linebyte, 0, len);
} else {
return new String(Linebyte, 0, len, charset);
}
} catch (Exception _ex) {
return null;
}
}

private String getFileName(String line) {
if (line == null) {
return "";
}

int i = line.indexOf("filename=");
line = line.substring(i + 9).trim();
i = line.lastIndexOf("\\");

if ((i < 0) || (i >= (line.length() - 1))) {
i = line.lastIndexOf("/");

if (line.equals("\"\"")) {
return "";
}

if ((i < 0) || (i >= (line.length() - 1))) {
return line;
}
}

return line.substring(i + 1, line.length() - 1);
}

private String getKey(String line) {
if (line == null) {
return "";
}

int i = line.indexOf("name=");
line = line.substring(i + 5).trim();

return line.substring(1, line.length() - 1);
}

private static byte[] arraycopy(byte[] source, int offset, int len) {
byte[] target = new byte[source.length];
System.arraycopy(source, offset, target, offset, len);
return target;
}

public void put(String key, String value) {
List<String> params = paramList.get(key);
if (params == null) {
params = new ArrayList<String>();
paramList.put(key, params);
}
params.add(value);
}

}




package com.huadi.system.beans.upload;

import javax.servlet.http.HttpServletRequest;

/**
* Created by IntelliJ IDEA. User: 王涛 Date: 2008-1-12 Time: 18:42:14 To change
* this template use File | Settings | File Templates.
*/
public class ProgressBar {
static String TAG = ".bar";
private long total = 10L;
private long nowaData;

private boolean finished = false;

public String getInfos() {
return infos;
}

public void setInfos(String infos) {
this.infos = infos;
}

private String infos = "";

private ProgressBar() {
}

public static ProgressBar getBar(HttpServletRequest request) {
ProgressBar bar = (ProgressBar) request.getSession(true).getAttribute(
TAG);
if (bar == null) {
bar = new ProgressBar();
request.getSession(true).setAttribute(TAG, bar);
}
return bar;
}

public static void clear(HttpServletRequest request) {
request.getSession(true).removeAttribute(TAG);
}

public long getTotal() {
return total;
}

public void setTotal(long total) {
this.total = total;
}

public long getNowaData() {
return nowaData;
}

public void setNowaData(long nowaData) {
this.nowaData = nowaData;
}

public boolean isFinished() {
return finished;
}

public void setFinished(boolean finished) {
this.finished = finished;
}

public String getPercent() {
return (nowaData * 100) / total + "";

}

public void clear() {
this.total = 1;
this.finished = false;
}
}




package com.huadi.system.mediadata.model;

/**
* Created by IntelliJ IDEA. User: 王涛 Date: 2008-1-12 Time: 19:55:34 To change
* this template use File | Settings | File Templates.
*/
public class UploadModel {
private String dir = "";
private String types;
private String maxSize = "10";
private String nameScheam = "default";

public String getDir() {
return dir;
}

public void setDir(String dir) {
this.dir = dir;
}

public String getTypes() {
return types;
}

public void setTypes(String types) {
this.types = types;
}

public String getMaxSize() {
return maxSize;
}

public void setMaxSize(String maxSize) {
this.maxSize = maxSize;
}

public String getNameScheam() {
return nameScheam;
}

public void setNameScheam(String nameScheam) {
this.nameScheam = nameScheam;
}
}




package com.huadi.system.beans;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import com.huadi.system.beans.upload.ProgressBar;
import com.huadi.system.beans.upload.naming.FileNaming;
import com.huadi.system.beans.upload.naming.NamingFactory;
import com.huadi.system.frame.exception.UploadErrorException;

/**
* Created by IntelliJ IDEA. User: 王涛 Date: 2008-1-12 Time: 14:40:37 To change
* this template use File | Settings | File Templates.
*/
public class FileUploadBean {
private String saveDir;
private String charset;
private long maxSize = 1024 * 1024 * 10;
private HttpServletRequest request;
private Map<String, List<String>> paramList = new HashMap<String, List<String>>();

private Set<String> fileTypes;
private String queryString;
private String boundary = "";
private String[] fileNames;
private String[] fileSizes;
private int len = 0;

private String userId = "";
private String userName = "";

private String nameScheam = "default";

private ProgressBar bar;

public void setNameScheam(String nameScheam) {
this.nameScheam = nameScheam;
}

public String getUserId() {
return userId;
}

public void setUserId(String userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getCharset() {
return charset;
}

public void setCharset(String charset) {
this.charset = charset;
}

public long getMaxSize() {
return maxSize;
}

public void setMaxSize(long maxSize) {
this.maxSize = maxSize;
}

public String getParameter(String name) {
List<String> params = paramList.get(name);
if (params == null || params.size() == 0) {
return null;
}
return params.get(0);
}

public String[] getParameterValues(String name) {
List<String> params = paramList.get(name);
if (params == null || params.size() == 0) {
return null;
}

String[] value = new String[params.size()];
return params.toArray(value);

}

public String getQueryString() {
return queryString;
}

public String[] getFileNames() {
return fileNames;
}

public FileUploadBean(String saveDir, HttpServletRequest request) {
this.saveDir = saveDir;
this.request = request;
File file = new File(this.saveDir);
if (!file.exists()) {
file.mkdirs();
}
boundary = this.request.getContentType().substring(30); // 得到内存中数据分界符
queryString = this.request.getQueryString();
bar = ProgressBar.getBar(request);
int size = request.getContentLength();

bar.setTotal(size);
}

public void addTyps(String type) {
if (fileTypes == null) {
fileTypes = new HashSet<String>();
}
fileTypes.add(type);
}

public void upload() throws ServletException, IOException,
UploadErrorException {
setCharset(request.getCharacterEncoding());
if (bar.getTotal() > maxSize)
throw new IOException("文件已超过最大值:" + maxSize / (1024 * 1024) + "M");

if (!uploadFile(request.getInputStream())) {
throw new UploadErrorException();
}
}

private boolean uploadFile(ServletInputStream servletinputstream)
throws IOException, UploadErrorException {
String line;
byte[] buffer = new byte[256];
List<String> _size = new ArrayList<String>();
byte[] lastbuffer;
int lastlen;
List<String> tmpFileName = new ArrayList<String>();
long size = 0L;
while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.startsWith("Content-Disposition: form-data;")) {
int i = line.indexOf("filename=");

if (i >= 0) {

String fName = getFileName(line);

if (fName.equals("")) {
continue;
}
if (fName.indexOf("\"") != -1) {
fName = fName.replaceAll("\"", "");
}
String ext = fName.substring((fName.lastIndexOf(".") + 1));

if ((fileTypes != null) && !fileTypes.contains(ext)) {
throw new UploadErrorException("上传文件的类型不允许!!");
}
String fn = fName.substring(0, fName.lastIndexOf("."));

tmpFileName.add(fName);
FileNaming scheam = NamingFactory.getNameScheam(nameScheam);
fName = scheam.getNextName(saveDir, fn, ext);

while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.length() <= 2) {
break;
}
}

File f = new File(fName);
FileOutputStream dos = new FileOutputStream(f);

lastbuffer = new byte[0];
lastlen = 0;

while ((line = readLine(buffer, servletinputstream, null)) != null) {
size += len;
bar.setNowaData(size);

if (line.indexOf(boundary) != -1) {
if (lastlen > 2) {
dos.write(lastbuffer, 0, lastlen - 2);
}

break;
}

dos.write(lastbuffer, 0, lastlen);
lastbuffer = arraycopy(buffer, 0, len);
lastlen = len;
}

BigDecimal bg = new BigDecimal((double) size
/ (1024 * 1024)).setScale(2,
BigDecimal.ROUND_HALF_UP);
scheam.saveInfos(userId, userName);
_size.add(bg.floatValue() + "");
dos.close();
} else {

String key = getKey(line);
String value = "";

while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.length() <= 2) {
break;
}
}

while ((line = readLine(buffer, servletinputstream, charset)) != null) {
if (line.indexOf(boundary) != -1) {
break;
}

value += line;
}

put(key, value.trim());
}
}
}

if (queryString != null) {
String[] each = queryString.split("&");
for (String s : each) {
String[] nv = s.split("=");
if (nv.length == 2) {
put(nv[0], nv[1]);
}
}
}

fileNames = new String[tmpFileName.size()];
fileNames = tmpFileName.toArray(fileNames);

fileSizes = new String[_size.size()];
fileSizes = _size.toArray(fileSizes);

return fileNames.length != 0;
}

/**
* 获取上传文件的大小
*
* @return
*/
public String[] getFileSizes() {
return fileSizes;
}

private String readLine(byte[] Linebyte,
ServletInputStream servletinputstream, String charset) {
try {
len = servletinputstream.readLine(Linebyte, 0, Linebyte.length);

if (len == -1) {
return null;
}

if (charset == null) {
return new String(Linebyte, 0, len);
} else {
return new String(Linebyte, 0, len, charset);
}
} catch (Exception _ex) {
return null;
}
}

private String getFileName(String line) {
if (line == null) {
return "";
}

int i = line.indexOf("filename=");
line = line.substring(i + 9).trim();
i = line.lastIndexOf("\\");

if ((i < 0) || (i >= (line.length() - 1))) {
i = line.lastIndexOf("/");

if (line.equals("\"\"")) {
return "";
}

if ((i < 0) || (i >= (line.length() - 1))) {
return line;
}
}

return line.substring(i + 1, line.length() - 1);
}

private String getKey(String line) {
if (line == null) {
return "";
}

int i = line.indexOf("name=");
line = line.substring(i + 5).trim();

return line.substring(1, line.length() - 1);
}

private static byte[] arraycopy(byte[] source, int offset, int len) {
byte[] target = new byte[source.length];
System.arraycopy(source, offset, target, offset, len);
return target;
}

public void put(String key, String value) {
List<String> params = paramList.get(key);
if (params == null) {
params = new ArrayList<String>();
paramList.put(key, params);
}
params.add(value);
}

}




<?xml version="1.0" encoding="gb2312"?>

<result id="202042" name="视频新闻" order="40" class="jdj_vodeopath_fj" uri="/actions/common/upload">
<returns>
<property name="default" value="" message="" type="forward"/>
<property name="failed" value="" message="" type="forward"/>
</returns>
<interceptors/>
<media-datas>
<property name="dir" value="/webgroup/jdj/spxw"/>
<property name="type" value=""/>
<property name="size" value="100"/>
<property name="scheam" value="default"/>
</media-datas>[code="java"]

</result>
[/code]



package com.huadi.system.mediadata;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.dom4j.Element;

import com.huadi.system.configs.ActionContext;
import com.huadi.system.mediadata.model.UploadModel;

/**
* Created by IntelliJ IDEA. User: 王涛 Date: 2008-1-12 Time: 14:08:58 To change
* this template use File | Settings | File Templates.
*/
public class UploadMediaData extends AbstractMediaData {
private Map<String, UploadModel> datas = new HashMap<String, UploadModel>();

public String getHtmlView(String uri, String resultid) {
StringBuffer sb = new StringBuffer(MediaDataHelper
.getTitle("文件上传元数据配置"));
UploadModel um = datas.get(resultid);
MediaDataHelper.uploadField(sb, um == null ? "" : um.getDir(),
um == null ? "" : um.getTypes(), um == null ? "" : um
.getMaxSize(), um == null ? "" : um.getNameScheam());
return sb.toString();
}

public void initDatas(Element root, String resultid) {
Element media = root.element("media-datas");
if (media == null)
return;
List list = media.elements("property");
UploadModel um = new UploadModel();
if (list != null && list.size() > 0) {
for (Object o : list) {
Element e = (Element) o;
String name = e.attribute("name").getValue();
String value = e.attribute("value").getValue();
if (name.equals("dir")) {
um.setDir(value);
} else if (name.equals("type")) {
um.setTypes(value);
} else if (name.equals("size")) {
um.setMaxSize(value);
} else if (name.equals("scheam")) {
um.setNameScheam(value);
}
}
}
datas.put(resultid, um);

}

public void initDatas(HttpServletRequest request, Element root) {
String dir = request.getParameter("dir");
String type = request.getParameter("filetype");
String size = request.getParameter("maxsize");
String scheam = request.getParameter("scheam");
if (scheam == null) {
scheam = "default";
}
Element media = root.addElement("media-datas");
media.addElement("property").addAttribute("name", "dir").addAttribute(
"value", dir);
media.addElement("property").addAttribute("name", "type").addAttribute(
"value", type);
media.addElement("property").addAttribute("name", "size").addAttribute(
"value", size);
media.addElement("property").addAttribute("name", "scheam")
.addAttribute("value", scheam);

}

public UploadModel getModel() {
return datas.get(ActionContext.getActionContext().getResultid());
}

public UploadModel getModel(String resultid) {
return datas.get(resultid);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值