当前位置:编程学习 > JAVA >>

求大神指点如何用js+SSH 实现下载功能 ? 下面是上传的实现

js:

jq("#btn_addition_add").click(function(){
  var value = jq("#jgid").val();
  if((value==null) || (value.Trim()=="")){
alert("没有进行入库申请,无法添加附件");
return false;
  }    
 var name = jq("input[name='upload']").val();
 if(name.Trim()==""){
   alert("请选择要上传的附件");
   return false;
 }
 jq("#form_addition").ajaxForm({ 
    url : "../zjjg/ajax_addAddition?id="+value,
dataType :  'json',  // pre-submit callback
        success :  processaddAddition
});
jq("#form_addition").submit();
   })
   
   function processaddAddition(data){
  if(data.message==null){
 alert("与服务器通信失败,请联系管理员"); 
  } else{  
    alert(data.message);
    if(data.result == 200){
     jq("input[name='upload']").val("");  
  jq("#table_addition tr:gt(0)").remove();
  if(data.additionList != null){
var index = 0;
    jq.each(data.additionList,function(i,value){
      index = i + 1;
              jq("#table_addition").append("<tr id='" +index +
             "'><td align='center'><input type='checkbox' id='chkaddition' value='"+value.additionid
             +"' ></input></td>" +
              "<td align='center' class='STYLE4'>"+index+"</td>" +
              "<td align='center' class='STYLE4'>"+value.name+"</td>" +
              "<td align='center' class='STYLE4'>"+changefilesize(value.filesize)+"</td>");  
    })
      } 
  }
       }
   }


strut.xml
<package name="zjjg" namespace="/zjjg" extends="json-default">
<action name="ajax_*"
class="com.hgzd.zjjgk.struts.action.jbxx.XxbgAction" method="{1}">
<result name="jbxx" type="json">
<param name="includeProperties">message,newid,result</param></result>
<result name="honor" type="json">
<param name="includeProperties">honorList\[\d+\]\.honorid,honorList\[\d+\]\.name,honorList\[\d+\]\.date,honorList\[\d+\]\.department,message,result</param></result>
<result name="typicalcase" type="json">
<param name="includeProperties">typicalcaseList\[\d+\]\.caseid,typicalcaseList\[\d+\]\.name,typicalcaseList\[\d+\]\.date,typicalcaseList\[\d+\]\.remark,message,result</param></result>
<result name="achievement" type="json">
<param name="includeProperties">achievementList\[\d+\]\.achievementid,achievementList\[\d+\]\.name,achievementList\[\d+\]\.date,achievementList\[\d+\]\.publication,achievementList\[\d+\]\.remark,message,result</param></result>
<result name="sincerity" type="json">
<param name="includeProperties">sincerityList\[\d+\]\.recordid,sincerityList\[\d+\]\.reason,sincerityList\[\d+\]\.remark,message,result</param></result>
<result name="zyryxx" type="json">
<param name="includeProperties">zyryxxList\[\d+\]\.id,zyryxxList\[\d+\]\.name,zyryxxList\[\d+\]\.gender,
   zyryxxList\[\d+\]\.post,zyryxxList\[\d+\]\.age,zyryxxList\[\d+\]\.education,zyryxxList\[\d+\]\.idnumber,
   zyryxxList\[\d+\]\.pcnumber,zyryxxList\[\d+\]\.pcdate,zyryxxList\[\d+\]\.cydate,
   message,result</param></result>
<result name="cyryxx" type="json">
<param name="includeProperties">cyryxxList\[\d+\]\.id,cyryxxList\[\d+\]\.name,cyryxxList\[\d+\]\.gender,
   cyryxxList\[\d+\]\.post,cyryxxList\[\d+\]\.age,cyryxxList\[\d+\]\.education,cyryxxList\[\d+\]\.idnumber,
   cyryxxList\[\d+\]\.cydate,result,message</param></result>
<result name="addition" type="json">
    <param name="contentType">text/html;charset=utf-8</param>
    <param name="includeProperties">additionList\[\d+\]\.additionid,additionList\[\d+\]\.name,additionList\[\d+\]\.filesize,message,result</param></result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action></package>

action:
public String addAddition() throws Exception {
this.setResult(0);
try {
final String id = ServletActionContext.getRequest().getParameter(
"id");
final Jbxxb zjjg = jbxxbService.getJbxxbById(Long.parseLong(id));
final String realPath = ServletActionContext.getRequest()
.getRealPath("/upload");
final File targetDirectory = CreateFileUtil.createDir(realPath
+ "\\" + zjjg.getJgid());
final Addition addition = new Addition();
addition.setName(uploadFileName);
addition.setFilepath(targetDirectory + "\\" + uploadFileName);
//
addition.setFilesize(upload.length());
addition.setFiletype(this.uploadContentType);
addition.setJbxxb(zjjg);
final File target = new File(targetDirectory, uploadFileName);
FileUtils.copyFile(upload, target);
final File newFile = new File(targetDirectory + "\\"
+ uploadFileName);
if (newFile.exists()) {
jbxxbService.saveAddition(addition);
final String sql = "select count(*) from Addition where jgid = "
+ id;
final Integer count = jbxxbService.findCountbySql(sql);
final String sql2 = "select * from Addition where jgid = " + id;
final List<Addition> list = (List<Addition>) jbxxbService
.getAdditionListbySql(count, sql2);
this.setAdditionList(list);
this.setMessage(conf.getValue("E049"));
this.setResult(200);
} else {
this.setMessage(conf.getValue("E047"));
}
} catch (final Exception e) {
this.setMessage(e.getMessage());
}
return "addition";
} --------------------编程问答-------------------- <iframe id="frmDownLoad" width="0" height="0"></iframe>

var url = "xxx.action?random="+Math.random();
document.getElementById("frmDownLoad").src=url;

后台:

/**
 * 下载文件
 * @param filePath 文件路径(物理路径)
 * @param fileName 源文件名称
 */
public void downLoadFile(String filePath, String fileName) {
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
return;
}
InputStream input =null;
OutputStream output = null;
try {
input = new FileInputStream(file);
output = getResponse().getOutputStream();
getResponse().setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
byte[] buffer = new byte[1024];
int i = 0;
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(null !=output) {
output.flush();
output.close();
}
if(null !=input) {
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
--------------------编程问答--------------------     主要是我要用到js  ,  要在js那边再利用url 跳转到struts.xml 中。  
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,