上传下载

做项目时用到上传记录一下以后好用。

这是页面内容

<body>
<iframe style="display:none" id="hiddenFrame" name="hiddenFrame" ></iframe>
<form id="frm" name="frm" method="post" enctype="multipart/form-data" target="hiddenFrame">
<table cellpadding="0" cellspacing="0" border="0" height="100%" width="100%" class="content_box">
 <tr>
     <td width="5" valign="top">&nbsp;</td>
      <td valign="top" align="center">
        <div class="title_div" style="width:70%"><font>项目基本信息</font></div>
            <table cellpadding="0" cellspacing="0" border="0" width="70%" class="list_tab_02" style="margin-top:10px;">
             
              <tr>
                <td ><p align="center">标题</p></td>
                <td align="left"><input type="text" id="title" name="title" <%if(dataList.size()>0){%>
                 value="<%=((Object[])dataList.get(0))[3].toString() %>"
                <%} %> readonly="readonly"/></td>
              </tr>
              <tr>
                <td ><p align="center">附件上传</p></td>
                 <td align="left">
                  <input id="qsclfj" name="qsclfj" type="hidden"/>
                  <input id="file1" type="file" multiple="multiple" />
                  <input id="btnupload" οnclick="upload()" type="button" value="上传" class="sc_btn" />
                 </td>
              </tr>
              <tr id="filetr" style="display:none">
                <td class="bg_04">附件列表:</td>
                    <td style="background-color:rgb(233,233,242)" id="fileList1" align="left"></td>
              </tr>
              <tr>
                <td ><p align="center">意见</p></td>
                 <td align="left"><textarea id="bz" name="bz" cols="20" rows="5"></textarea></td>
              </tr>
            </table>
  <div class="button_div" style="width:70%;">
  <img src="<%=cp %>/style/images/button_tj_img.gif" οnclick="save()"/>
  <img src="<%=cp %>/style/images/button_ht_img.gif" οnclick="back()"/>
  </div>
   </td>
        <td width="5" valign="top">&nbsp;</td>
    </tr>
</table>
</form>
</body>

Javascript事件

function upload(){
   var nsrglm = document.getElementById("nsrglm").value;
   var xmbm = document.getElementById("xmbm").value;
   var url = "<%=cp%>/upload/uploadFilesDo?nsrglm="+nsrglm+"&xmbm="+xmbm;
   uploadfiles(url,'<%=cp %>','file1','frm','qsclfj','fileList1','filetr');
  }

后台代码

public class UploadFilesAction extends ActionSupport {

 private static final long serialVersionUID = 1L;
 
 public static final SimpleDateFormat sdformat = new SimpleDateFormat("yyyyMMdd");
 
 // 对应文件域的file名称,封装文件内容
 private File[] file;
 // 封装文件类型
 private String[] fileContentType;
 // 封装文件名
 private String[] fileFileName;

 /**
  * 文件拷贝
  * @param sourceFile
  * @param destFile
  */
 private void copyFile(File sourceFile, File destFile) {
  FileInputStream fin = null;
  FileOutputStream fout = null;
  try {
   int c;
   byte[] b = new byte[1024 * 5];
   fin = new FileInputStream(sourceFile);
   fout = new FileOutputStream(destFile);
   while ((c = fin.read(b)) != -1) {
    fout.write(b, 0, c);
   }
   fout.flush();
  } catch (Exception e) {
   //
  } finally {
   try {
    fin.close();
    fout.close();
   } catch (IOException e) {
    //
   }

  }

 }

 /**
  * 上传附件的通用方法
  *
  * @return
  * @throws Exception
  */
 @SuppressWarnings("unchecked")
 public void uploadFiles() throws Exception {
  try {
   HttpServletRequest request = ServletActionContext.getRequest();
   HttpServletResponse response = ServletActionContext.getResponse();
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   Map retMap = new HashMap();
   StringBuffer json = new StringBuffer();
   // String forward = request.getParameter(TaxConstants.FORWARD_NEXT);
   String nsrglm = request.getParameter("nsrglm");
   String xmbm = request.getParameter("xmbm");
   String realpath = FileUtil.getFileSaveRealPath();
   String childDirPath = nsrglm + "/" + xmbm;
   String fileFolder = realpath + "/" + childDirPath;
   FileUtil.isExistFolder(fileFolder);// 判断是否存在目录,不存在创建
   if (file != null && file.length > 0) {
    for (int i = 0; i < file.length; i++) {
     File destFile = new File(fileFolder, fileFileName[i]);
     if (!destFile.exists()) {
      destFile.createNewFile();
     }
     copyFile(file[i], destFile);
    }
    retMap.put("success", "true");
    retMap.put("flag", "ok");
    retMap.put("msg", "上传成功!");
    retMap.put("fileName", fileFileName[0]);
    retMap.put("childDir", childDirPath);
    json
      .append("{\"success\":\"" + retMap.get("success")
        + "\",\"flag\":\"" + retMap.get("flag")
        + "\",\"msg\":\"" + retMap.get("msg")
        + "\",\"fileName\":\"" + retMap.get("fileName")
        + "\",\"childDir\":\"" + retMap.get("childDir")
        + "\"}");
    out.print(json.toString());
   } else {
    retMap.put("flag", "fail");
    retMap.put("msg", "不提供所请求的服务.");
    retMap.put("success", "false");
    json.append("{\"success\":\"" + retMap.get("success")
      + "\",\"flag\":\"" + retMap.get("flag")
      + "\",\"msg\":\"" + retMap.get("msg") + "\"}");
    out.print(json.toString());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  //RequestDispatcher rd = request.getRequestDispatcher(forward);
  //rd.forward(request, response);
 }
 
 @SuppressWarnings("unchecked")
 public void deleteFiles() throws IOException{
  try{
  HttpServletRequest request = ServletActionContext.getRequest();
  HttpServletResponse response = ServletActionContext.getResponse();
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter();
  Map retMap = new HashMap();
  StringBuffer json = new StringBuffer();
  String filePath = request.getParameter("filePath");
  File file = new File(FileUtil.getFileSaveRealPath() + "/" + filePath);
  if(!file.isDirectory()){
   if(file.delete()){
    retMap.put("flag", "ok");
    retMap.put("msg", "删除文件成功!.");
    retMap.put("success", "true");
    json.append("{\"success\":\""+ retMap.get("success") +"\",\"flag\":\""+ retMap.get("flag") +"\",\"msg\":\""+ retMap.get("msg") +"\"}");
    out.print(json.toString());
   }else{
    retMap.put("flag", "fail");
    retMap.put("msg", "不提供所请求的服务.");
    retMap.put("success", "false");
    json.append("{\"success\":\""+ retMap.get("success") +"\",\"flag\":\""+ retMap.get("flag") +"\",\"msg\":\""+ retMap.get("msg") +"\"}");
    out.print(json.toString());
   }
   
  }else{
   retMap.put("flag", "fail");
   retMap.put("msg", "不提供所请求的服务.");
   retMap.put("success", "false");
   json.append("{\"success\":\""+ retMap.get("success") +"\",\"flag\":\""+ retMap.get("flag") +"\",\"msg\":\""+ retMap.get("msg") +"\"}");
   out.print(json.toString());
  }
  }catch(Exception e){
   e.printStackTrace();
  }
 }

 public File[] getFile() {
  return file;
 }

 public void setFile(File[] file) {
  this.file = file;
 }

 public String[] getFileContentType() {
  return fileContentType;
 }

 public void setFileContentType(String[] fileContentType) {
  this.fileContentType = fileContentType;
 }

 public String[] getFileFileName() {
  return fileFileName;
 }

 public void setFileFileName(String[] fileFileName) {
  this.fileFileName = fileFileName;
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值