JEECG表单附件的上传和下载

本文介绍了如何在JEECG系统中实现文件上传和下载功能。通过使用框架提供的 `<t:upload>` 标签进行两次请求,首先完成文件上传,然后将上传成功的文件路径保存。下载文件时,通过特定的Controller处理请求,根据文件类型设置响应头并提供下载。文中详细展示了上传和下载的相关代码实现。

最近刚接触到jeecg(因为是‘’快餐”系统)老大叫我实现文件上传和显示的功能。

遇到的问题:用spring的文件上传器在表单提交的Controller里同时存文件,并把附件地址保存的时候发现该表单<input type="file">文件内容在Contrller里面读不到。

解决方案: 用框架封装好的 <t:upload 进行2次请求

<t:upload name="instruction" dialog="false" queueID="instructionfile"  auto="true" uploader="fileController.do?upload" extend="*.pic;*.doc;*.txt;*.xls;*.zip" id="instruction" formData="documentTitle" onUploadSuccess="uploadSuccess" buttonText="选择附件">
</t:upload>

第一次:选择文件后自动提交到fileController.do?upload 里面代码如下

/**
* 上传附件功能跳转

* @return
*/
@RequestMapping(params = "upload", method=RequestMethod.POST)
@ResponseBody
public AjaxJson upload(HttpServletRequest req) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)req;
Iterator iter= multipartRequest.getFileNames();
String path =null;
AjaxJson j = new AjaxJson();
String name= "";
while(iter.hasNext()){
MultipartFile file = multipartRequest.getFile(iter.next().toString());
name= file.getOriginalFilename();
path = req.getSession().getServletContext().getRealPath("/")+"upload/"+name;
try {
file.transferTo(new File(path));
} catch (Exception e) {
j.setMsg("附件上传失败!");
return j;
}
}
j.setMsg("附件上传成功!");
j.setObj(name);
return j;
}


这里返回一个AJax对象。在我的了解中ajax是不能上传文件和下载文件的(只能传输字符串,文件流是不可以的)。

后台存文件成功后会调用uploadSuccess这个方法!(onUploadSuccess="uploadSuccess")

js:function uploadSuccess(d,file,response){
                $("#pathurl").val(d.obj);
             /*  console.log($("#instruction").val()); 
                console.log($("#pathurl").val()); */
                $("#hidden_div").show();
     }

成功后把path的值赋给表单里面的pathurl对象,然后显示提示(虽然很丑。。)。


下载文件:<a href="fileController.do?download&path=${complainsLetterPage.pathurl}" >附件下载</a>

点击一个<a></a>标签然后进入对于Contrller

@RequestMapping(params = "download")
@ResponseBody
public AjaxJson download(HttpServletRequest request, HttpServletResponse response) {
String aa= request.getCharacterEncoding();
String name= request.getParameter("path");
AjaxJson j= new AjaxJson();
        java.io.BufferedInputStream bis = null;
        java.io.BufferedOutputStream bos = null;
        String path = request.getSession().getServletContext().getRealPath("/")+"upload/"+name;
        String type = name.substring(name.indexOf(".")); 
        try {
        long fileLength = new File(path).length();
        if(".png".equals(type)){
        response.setContentType("application/x-png;");
                response.setHeader("Content-disposition", "attachment; filename="
                        + new String(name.getBytes("utf-8"), "iso8859-1"));
        }
        if(".jpg".equals(type)){
        response.setContentType("image/jpg;");
                response.setHeader("Content-disposition", "attachment; filename="
                        + new String(name.getBytes("utf-8"), "iso8859-1"));
        }
        if(".doc".equals(type)){
        response.setContentType("application/msword;");
                response.setHeader("Content-disposition", "attachment; filename="
                        + new String(name.getBytes("utf-8"), "iso8859-1"));
        }
        if(".txt".equals(type)){
        response.setContentType("text/xml;");
                response.setHeader("Content-disposition", "attachment; filename="
                        + new String(name.getBytes("utf-8"), "iso8859-1"));
        }
        if(".xls".equals(type)){
        response.setContentType("application/x-xls;");
                response.setHeader("Content-disposition", "attachment; filename="
                        + new String(name.getBytes("utf-8"), "iso8859-1"));
        }
        if(".zip".equals(type)){
        response.setContentType("application/x-zip-compressed;");
                response.setHeader("Content-disposition", "attachment; filename="
                        + new String(name.getBytes("utf-8"), "iso8859-1"));
        }
            response.setHeader("Content-Length", String.valueOf(fileLength));
            bis = new BufferedInputStream(new FileInputStream(path));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
   
} catch (Exception e) {
// TODO: handle exception
}finally {
            if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
            if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
        }
        j.setMsg("下载成功");
        return j;

}

这里如果 response.setContentType不设置的话,如果是图片直接前台直接回弹出一个框框显示图片。OK!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值