jspsmartupload学习与应用

我直接用struts进行编程,后来换一家公司,就是基本的jsp操作,碰到文件上传就只能自己写了,后来找到jspsmartupload.jar,原来没有jar也没有源文件.使用起来也方便,就自己总结了一下:
1、jspsmartupload不大,不懂可以直接查一下原代码。
2、上传文件,addFile.jsp
其中:form要有enctype="multipart/form-data"才可以上传附件
  1. <form method="post" name="myform" action="doAddAbortReport.jsp" enctype="multipart/form-data">
  2. <table width="100%"  border="0" cellpadding="0" cellspacing="0">
  3. <tr>
  4.  <td class="td_table_1">故障组网简图</td>
  5.  <td class="td_table_2">
  6. <script>
  7. var abortAcceptfilei = 0;
  8. function addFile(){
  9.  var row1 = document.all.tableFile.insertRow();
  10.  var cell1 = row1.insertCell(0);
  11.  abortAcceptfilei++;
  12.  row1.id = "id_" + abortAcceptfilei;
  13.  cell1.innerHTML = "<input type='file' class='" + "input_file" + "' name='file[" + abortAcceptfilei + "]' size='40' />   <input type='button' class='unnamed_anliu' value='删除' onclick=delFile('" + row1.id + "')>";
  14. }
  15. function delFile(f){
  16.  document.all.tableFile.deleteRow(
  17.   document.getElementById(f).rowIndex);
  18. }
  19. </script>
  20. <input type="button" class="unnamed_anliu2" value="增加附件" onclick="addFile();">
  21. <table border="0" class="table_all" id="tableFile">
  22. </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是我们项目中的被修改过.将类名也修改了,但是方法是一样的.
  1. <table class="table_all" width="100%"  border="0" cellpadding="0" cellspacing="0">
  2. <tr><td class="td_table_top2">分析报告</td></tr>
  3. <%
  4. DkhReportMgr mgr = new DkhReportMgr();
  5. DkhReportInfo info=new DkhReportInfo();
  6. SmartUpload su=new SmartUpload();
  7. String draftid ="";
  8. String dkhId = "";
  9. String chaxun ="";
  10. String flag = "";
  11. String save = "";
  12. try{
  13.  su.initialize(pageContext);
  14.  // 1.限制每个上传文件的最大长度。
  15.  su.setMaxFileSize(100000);
  16.  // 2.限制总上传数据的长度。
  17.  su.setTotalMaxFileSize(200000);
  18.  // 3.设定允许上传的文件。
  19.  su.setAllowedFilesList("gif,jpg");
  20.  // 4.设定禁止上传的文件。
  21.  su.setDeniedFilesList("exe,bat,jsp,htm,html");
  22.  su.upload();
  23.  int count = su.save("/upload");
  24.  out.print("<tr><td class=/"td_table_1/">"+count+"个文件附件上传成功!<br>");
  25.  String imgs="";
  26.  for (int i=0;i<su.getFiles().getCount();i++) {
  27.   com.jspsmart.upload.File file = su.getFiles().getFile(i);
  28.   //若文件不存在则继续
  29.   if (file.isMissing()) continue;
  30.   imgsimgs=imgs+request.getContextPath()+"/upload/"+file.getFileName()+";";
  31.  }
  32.  info.setImgs(imgs);
  33.  com.jspsmart.upload.Request req=su.getRequest();
  34.  draftid = req.getParameter("draftid");
  35.  dkhId = req.getParameter("dkhId");
  36.  chaxun = req.getParameter("chaxun");
  37.  flag = req.getParameter("flag");
  38.  save = req.getParameter("save");
  39.  info.setDraftid(req.getParameter("draftid"));
  40.  info.setBiaoti(req.getParameter("biaoti"));
  41.  info.setBianhao(req.getParameter("bianhao"));
  42.  info.setDldh(req.getParameter("dldh"));
  43.  info.setKhmc(req.getParameter("khmc"));
  44.  info.setGzly(req.getParameter("gzly"));
  45.  info.setYwlx(req.getParameter("ywlx"));
  46.  info.setSsds(req.getParameter("ssds"));
  47.  info.setStarttime(req.getParameter("starttime"));
  48.  info.setEndtime(req.getParameter("endtime"));
  49.  info.setCounttime(req.getParameter("counttime"));
  50.  info.setYxfwsm(req.getParameter("yxfwsm"));
  51.  info.setJyms(req.getParameter("jyms"));
  52.  info.setZjxx(req.getParameter("zjxx"));
  53.  info.setFxclms(req.getParameter("fxclms"));
  54.  info.setCsff(req.getParameter("csff"));
  55.  info.setCsjg(req.getParameter("csjg"));
  56.  info.setCqcs(req.getParameter("cqcs"));
  57.  info.setBgdw(req.getParameter("bgdw"));
  58.  info.setBgdate(req.getParameter("bgdate"));
  59.  mgr.saveDkhReportInfo(info);
  60.  out.print("<tr><td class=/"td_table_1/">分析报告上传成功!<br>");
  61. }catch (Exception e) {
  62.  out.print("<tr><td class=/"td_table_1/">"+e.getMessage()+"<br>");
  63. }
  64. %>
  65. <tr>
  66.  <td  class="td_table_bottom">
  67.  <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>
  68. </tr>
  69. </table>
最后,将我原来自己写的文件上传代码放上来,作为参考:
这个方法用在解析将要上传的excel文件,将excel文件以字节码的形式放(记住去掉后面多余的两个字节).
使用方法:
  1. byte[] by = getFileContent("file", request.getInputStream());
  2. ByteArrayInputStream bin = new ByteArrayInputStream(by, 0,by.length-2);
  3. HSSFWorkbook workbook = new HSSFWorkbook(bin);
  4. //以下是类中的几个方法.
  5. private byte[] getFileContent(String name, InputStream is)
  6.  throws IOException {
  7.  int index;
  8.  boolean isEnd = false;
  9.  byte[] lineSeparatorByte;
  10.  byte[] lineData;
  11.  String content_disposition;
  12.  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  13.  BufferedInputStream bis = new BufferedInputStream(is);
  14.  lineSeparatorByte = readStreamLine(bis);
  15.  while (!isEnd) {
  16.   lineData = readStreamLine(bis);
  17.   if (lineData == null) {
  18.    break;
  19.   }
  20.   content_disposition = new String(lineData, "ASCII");
  21.   index = content_disposition.indexOf("name=/"" + name + "/"");
  22.   if (index >= 0 && index < content_disposition.length()) {
  23.    readStreamLineAsString(bis); // skip a line
  24.    readStreamLineAsString(bis); // skip a line
  25.    while ((lineData = readStreamLine(bis)) != null) {
  26.     if (isByteArraystartWith(lineData, lineSeparatorByte)) { // end
  27.      isEnd = true;
  28.      break;
  29.     } else {
  30.      bos.write(lineData);
  31.     }
  32.    }
  33.   } else {
  34.    lineData = readStreamLine(bis);
  35.    if (lineData == null)
  36.     return null;
  37.    while (!isByteArraystartWith(lineData, lineSeparatorByte)) {
  38.     lineData = readStreamLine(bis);
  39.     if (lineData == null)
  40.      return null;
  41.    }
  42.   }
  43.  }
  44.  return bos.toByteArray();
  45. }
  46. private boolean isByteArraystartWith(byte[] arr, byte[] pat) {
  47.  int i;
  48.  if (arr == null || pat == null)
  49.   return false;
  50.  if (arr.length < pat.length)
  51.   return false;
  52.  for (i = 0; i < pat.length; i++) {
  53.   if (arr[i] != pat[i])
  54.    return false;
  55.  }
  56.  return true;
  57. }
  58. private byte[] readStreamLine(BufferedInputStream in) throws IOException {
  59.  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  60.  int b = in.read();
  61.  if (b == -1)
  62.   return null;
  63.  while (b != -1) {
  64.   bos.write(b);
  65.   if (b == '/n')
  66.    break;
  67.   b = in.read();
  68.  }
  69.  return bos.toByteArray();
  70. }
  71. private String readStreamLineAsString(BufferedInputStream in)
  72.  throws IOException {
  73.  return new String(readStreamLine(in), "ASCII");



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值