java文件上传下载


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
     *  上传文档
     * @param upExcel 
     * @return
     */
    @RequestMapping(value="/formExcel",method = RequestMethod.POST)
    @ResponseBody
    public void formExcel(MultipartFile upExcel,String fileName ){

        System.out.println(upExcel.getOriginalFilename()+"----"+fileName);
        try {
            String originalFilename = fileName;
            if(originalFilename==null||originalFilename==""){
//              return "请选择要上传的文件";
//              this.renderJson(json(null,1,"请选择要上传的文件"));
                this.printFailure(1, "请选择要上传的文件");
                return;
            }

            if(originalFilename.endsWith(".doc")||originalFilename.endsWith(".docx")||
                    originalFilename.endsWith(".pdf")||originalFilename.endsWith(".xlsx")||originalFilename.endsWith(".xls")||
                    originalFilename.endsWith(".ppt")||originalFilename.endsWith(".pptx")){

                String uploadPath = "upload/document"+ "/" + UUID.getUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
    //          String uploadPath = "upload/document"+ "/" + UUID.getUUID()+originalFilename;

                    FileStorageHelper.transferFile(upExcel.getInputStream(), uploadPath);
                    Document document = new Document();

                    String[] split = originalFilename.split("\\.");
                    String string = split[0];

                    document.setDesn(string);
                    document.setUrl(uploadPath);
                    String id = UUID.getUUID();
                    document.setAuthor(UserTokenHelper.getUserId());
                    document.setDocumentID(id);
                    documentService.save(document);
//                  return id;
//                  this.renderJson(json(id,0,"上传文件成功"));
                    this.printSuccess(id,"上传文件成功");
                    return;
            }else{
//              return "请选择正确格式的文档";
//              this.renderJson(json(null,2,"请选择正确格式的文档"));
                this.printFailure(2,"请选择正确格式的文档");

                return;
            }
        } catch (Exception e) {
            e.printStackTrace();

//          return "上传文件失败";
//          this.renderJson(json(null,3,"上传文件失败"));
            this.printFailure(3,"上传文件失败");
            return;
        }
    }

下载文件

/**

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
        *  下载文件
        * @param urldata 文件路径
        * @param response
        */
       @RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/json")
       @ResponseBody
    public void download(String urldata,String name,HttpServletResponse response){

//         String rootPath = SpringMVCUtil.getRequest().getSession().getServletContext().getRealPath("/");// 获取项目根目录

            try {
                File file = new File(urldata);// path是根据日志路径和文件名拼接出来的
                String filename = file.getName();// 获取文件名称
                String[] split = filename.split("\\.");
                String string = split[1];
                String nameNew = name+"."+string;
                InputStream fis = new BufferedInputStream(new FileInputStream(urldata));
                byte[] buffer = new byte[fis.available()];
                fis.read(buffer);
                fis.close();
                response.reset();
                // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名
                //response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
                response.addHeader("Content-Disposition", "attachment;filename=" + new String(nameNew.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1"));
                response.addHeader("Content-Length", "" + file.length());
                OutputStream os = new BufferedOutputStream(response.getOutputStream());
                response.setContentType("application/octet-stream");
                os.write(buffer);// 输出文件
                os.flush();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值