您的位置:首页 > Web前端 > JavaScript

jspsmartupload学习与应用

2007-10-17 17:14 316 查看
我直接用struts进行编程,后来换一家公司,就是基本的jsp操作,碰到文件上传就只能自己写了,后来找到jspsmartupload.jar,原来没有jar也没有源文件.使用起来也方便,就自己总结了一下:
1、jspsmartupload不大,不懂可以直接查一下原代码。
2、上传文件,addFile.jsp
其中:form要有enctype="multipart/form-data"才可以上传附件
<form method="post" name="myform" action="doAddAbortReport.jsp" enctype="multipart/form-data">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="td_table_1">故障组网简图</td>
<td class="td_table_2">
<script>
var abortAcceptfilei = 0;
function addFile(){
var row1 = document.all.tableFile.insertRow();
var cell1 = row1.insertCell(0);
abortAcceptfilei++;
row1.id = "id_" + abortAcceptfilei;
cell1.innerHTML = "<input type='file' class='" + "input_file" + "' name='file[" + abortAcceptfilei + "]' size='40' /> <input type='button' class='unnamed_anliu' value='删除' onclick=delFile('" + row1.id + "')>";
}
function delFile(f){
document.all.tableFile.deleteRow(
document.getElementById(f).rowIndex);
}
</script>
<input type="button" class="unnamed_anliu2" value="增加附件" onclick="addFile();">
<table border="0" class="table_all" id="tableFile">
</table>
3、处理上传文件的jsp
注意:
(1)定义的SmartUpload对像要进行初始化
su.initialize(pageContext);
(2)页面的参数取到,要用的话就用com.jspsmart.upload.Request req=su.getRequest();
req.getParameter("draftid");
(3)如果要初始化,必须是enctype="multipart/form-data"类型的页面提交过来才可以,一般的形式会报错.
(4)附件上传上去后,通过su.getFiles().getFile(i);取行文件名及上传的前的路径,上传后的路径我是自己组合起来的,这个里面好像没有。
(5)我上传的jspsmartupload是我们项目中的被修改过.将类名也修改了,但是方法是一样的.
<table class="table_all" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td class="td_table_top2">分析报告</td></tr>
<%
DkhReportMgr mgr = new DkhReportMgr();
DkhReportInfo info=new DkhReportInfo();
SmartUpload su=new SmartUpload();
String draftid ="";
String dkhId = "";
String chaxun ="";
String flag = "";
String save = "";
try{
su.initialize(pageContext);
// 1.限制每个上传文件的最大长度。
su.setMaxFileSize(100000);
// 2.限制总上传数据的长度。
su.setTotalMaxFileSize(200000);
// 3.设定允许上传的文件。
su.setAllowedFilesList("gif,jpg");
// 4.设定禁止上传的文件。
su.setDeniedFilesList("exe,bat,jsp,htm,html");
su.upload();
int count = su.save("/upload");
out.print("<tr><td class=/"td_table_1/">"+count+"个文件附件上传成功!<br>");
String imgs="";
for (int i=0;i<su.getFiles().getCount();i++) {
com.jspsmart.upload.File file = su.getFiles().getFile(i);
//若文件不存在则继续
if (file.isMissing()) continue;
imgsimgs=imgs+request.getContextPath()+"/upload/"+file.getFileName()+";";
}
info.setImgs(imgs);
com.jspsmart.upload.Request req=su.getRequest();
draftid = req.getParameter("draftid");
dkhId = req.getParameter("dkhId");
chaxun = req.getParameter("chaxun");
flag = req.getParameter("flag");
save = req.getParameter("save");
info.setDraftid(req.getParameter("draftid"));
info.setBiaoti(req.getParameter("biaoti"));
info.setBianhao(req.getParameter("bianhao"));
info.setDldh(req.getParameter("dldh"));
info.setKhmc(req.getParameter("khmc"));
info.setGzly(req.getParameter("gzly"));
info.setYwlx(req.getParameter("ywlx"));
info.setSsds(req.getParameter("ssds"));
info.setStarttime(req.getParameter("starttime"));
info.setEndtime(req.getParameter("endtime"));
info.setCounttime(req.getParameter("counttime"));
info.setYxfwsm(req.getParameter("yxfwsm"));
info.setJyms(req.getParameter("jyms"));
info.setZjxx(req.getParameter("zjxx"));
info.setFxclms(req.getParameter("fxclms"));
info.setCsff(req.getParameter("csff"));
info.setCsjg(req.getParameter("csjg"));
info.setCqcs(req.getParameter("cqcs"));
info.setBgdw(req.getParameter("bgdw"));
info.setBgdate(req.getParameter("bgdate"));
mgr.saveDkhReportInfo(info);
out.print("<tr><td class=/"td_table_1/">分析报告上传成功!<br>");
}catch (Exception e) {
out.print("<tr><td class=/"td_table_1/">"+e.getMessage()+"<br>");
}
%>
<tr>
<td class="td_table_bottom">
<input type="button" class="unnamed_anliu" value="返 回" name="submit_sulv" onclick="javascript:window.location.href('viewAbortReport.jsp?draftid=<%=draftid%>&dkhId=<%=dkhId%>&chaxun=<%=chaxun%>&flag=<%=flag%>');"></td>
</tr>
</table>
最后,将我原来自己写的文件上传代码放上来,作为参考:
这个方法用在解析将要上传的excel文件,将excel文件以字节码的形式放(记住去掉后面多余的两个字节).
使用方法:
byte[] by = getFileContent("file", request.getInputStream());
ByteArrayInputStream bin = new ByteArrayInputStream(by, 0,by.length-2);
HSSFWorkbook workbook = new HSSFWorkbook(bin);
//以下是类中的几个方法.
private byte[] getFileContent(String name, InputStream is)
throws IOException {
int index;
boolean isEnd = false;
byte[] lineSeparatorByte;
byte[] lineData;
String content_disposition;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
BufferedInputStream bis = new BufferedInputStream(is);

lineSeparatorByte = readStreamLine(bis);
while (!isEnd) {
lineData = readStreamLine(bis);
if (lineData == null) {
break;
}
content_disposition = new String(lineData, "ASCII");
index = content_disposition.indexOf("name=/"" + name + "/"");
if (index >= 0 && index < content_disposition.length()) {
readStreamLineAsString(bis); // skip a line
readStreamLineAsString(bis); // skip a line
while ((lineData = readStreamLine(bis)) != null) {
if (isByteArraystartWith(lineData, lineSeparatorByte)) { // end
isEnd = true;
break;
} else {
bos.write(lineData);
}
}
} else {
lineData = readStreamLine(bis);
if (lineData == null)
return null;
while (!isByteArraystartWith(lineData, lineSeparatorByte)) {
lineData = readStreamLine(bis);
if (lineData == null)
return null;
}
}
}
return bos.toByteArray();
}

private boolean isByteArraystartWith(byte[] arr, byte[] pat) {
int i;
if (arr == null || pat == null)
return false;
if (arr.length < pat.length)
return false;
for (i = 0; i < pat.length; i++) {
if (arr[i] != pat[i])
return false;
}
return true;
}

private byte[] readStreamLine(BufferedInputStream in) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int b = in.read();
if (b == -1)
return null;
while (b != -1) {
bos.write(b);
if (b == '/n')
break;
b = in.read();
}
return bos.toByteArray();
}

private String readStreamLineAsString(BufferedInputStream in)
throws IOException {
return new String(readStreamLine(in), "ASCII");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: