java利用joup,imageio,base64来实现简历下载为html格式。

1 需要的依赖包

<!--html操作工具-->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.10.2</version>
</dependency>

2 远程图片转为base64编码

import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ImagesToBase64{

    public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        ByteArrayOutputStream outputStream = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imageUrl);
            outputStream = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStream);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串
    }

}

Base64 在CSS中的使用

.demoImg{ background-image: url("data:image/jpg;base64,/9j/4QMZRXhpZgAASUkqAAgAAAAL....");}

Base64 在HTML中的使用

<img width="40" height="30" src="data:image/jpg;base64,/9j/4QMZRXhpZgAASUkqAAgAAAAL...." />

3 使用springmv来接收请求并打开浏览器下载框(不支持ajax请求)

/**
 *
 * 下载简历为html
 * author Miss Xiao
 * date 2017.04.25 20:21
 */
@RequestMapping("downloadFile")
public void downloadFile(HttpServletResponse response, HttpServletRequest request){

   //基础信息
   RmResumeBasiVo rmResumeBasiVo = rmResumeBasiService.selectResumeBaseVoByType(super.getUseNo(),1);
   if (null!=rmResumeBasiVo) {
      Long resumeNo = rmResumeBasiVo.getResumeNo();
      //意向行业
      RmResumeIntentionVo rmResumeIntentionVo = rmResumeIntentionService.findOneVo(resumeNo);
      RmResumeJobVo jobVo = new RmResumeJobVo();
      jobVo.setResumeNo(resumeNo);
      //工作经验集合
      List<RmResumeJobVo> jobVos = rmResumeJobService.findListVo(jobVo);
      //教育背景
      RmResumeEducationVo rmResumeEducationVo = rmResumeEducationService.findOneVo(resumeNo);
      Date date = new Date();
      //打开下载框(谷歌无效)
      response.setCharacterEncoding("utf-8");
      response.setContentType("multipart/form-data");
      response.setHeader("Content-Disposition", "attachment;fileName="+date.getTime()+".html");
      String url = request.getServletContext().getRealPath("/WEB-INF/pages/common/resume_template.html");
      try {
         File file=new File(url);
         InputStream inputStream=new FileInputStream(file);
         //获取html
         Document doc = Jsoup.parse(inputStream,"UTF-8","http://www.oschina.net/");


         //插入基础信息
         doc.getElementById("realName").text(rmResumeBasiVo.getRealName());
         doc.getElementById("headPortrait").attr("src", "data:image/jpg;base64,"+ImagesToBase64.encodeImgageToBase64(new URL(rmResumeBasiVo.getHeadPortrait())));
         doc.getElementById("age").text("24");
         doc.getElementById("nowLive").text(rmResumeBasiVo.getNowLive());
         doc.getElementById("mobilePhone").text(rmResumeBasiVo.getMobilePhone());
         doc.getElementById("email").text(rmResumeBasiVo.getEmail());

         //插入意向行业信息
         if (null!=rmResumeIntentionVo){
            doc.getElementById("industry").text(rmResumeIntentionVo.getIndustry());
            doc.getElementById("post").text(rmResumeIntentionVo.getPost());
            doc.getElementById("region").text(rmResumeIntentionVo.getRegion());
            doc.getElementById("salary").text(rmResumeIntentionVo.getSalary().toString());
            String housing = rmResumeIntentionVo.getHousing()==0?"不提供住宿":"提供住宿";
            String arrivalTime = rmResumeIntentionVo.getArrivalTime()==0?"随时上岗":"电话通知";
            doc.getElementById("housing").text(housing);
            doc.getElementById("arrivalTime").text(arrivalTime);
         }else {
            doc.getElementById("job_intension").attr("hidden","hidden");
         }
         //插入教育背景信息
         if (null!=rmResumeEducationVo){
            doc.getElementById("graduateTime").text(new SimpleDateFormat("yyyy.MM.dd").format(rmResumeEducationVo.getGraduateTime()));
            doc.getElementById("school").text(rmResumeEducationVo.getSchool());
            String edu = "暂无";
            if (rmResumeEducationVo.getEducation()=="0"){
               edu = "大专以下";
            }else if (rmResumeEducationVo.getEducation()=="1"){
               edu = "大专";
            }else if (rmResumeEducationVo.getEducation()=="2"){
               edu = "本科";
            }else if (rmResumeEducationVo.getEducation()=="3"){
               edu = "硕士";
            }else if (rmResumeEducationVo.getEducation()=="4"){
               edu = "博士";
            }
            doc.getElementById("education").text(edu);
            doc.getElementById("describes").text(rmResumeEducationVo.getDescribes());
         }else {
            doc.getElementById("educational_background").attr("hidden","hidden");
         }
         //插入工作经验信息
         if (jobVos.size()>0){
                  for (RmResumeJobVo lis:jobVos){
                  String htm = " <dd class=\"clearfix resume_append_area\">\n" +
                  "                        <div class=\"conTitle\">\n" +
                  "                            在岗时间:\n" +
                  "                            <i contenteditable=\"true\" class=\"resume_time\">"+new SimpleDateFormat("yyyy.MM.dd").format(lis.getBeginTime())+"</i>&nbsp;\n" +
                  "                            <i contenteditable=\"true\" class=\"resume_time\">"+new SimpleDateFormat("yyyy.MM.dd").format(lis.getEndTime())+"</i>\n" +
                  "                            &nbsp; 公司名称:\n" +
                  "                            <i contenteditable=\"true\" class=\"resume_unit\">"+lis.getEnterpriseName()+"</i>\n" +
                  "                            &nbsp;  职位:\n" +
                  "                            <i contenteditable=\"true\" class=\"resume_job\">"+lis.getIndustry()+"</i>\n" +
                  "                        </div>\n" +
                  "                        <div >\n" +
                  "                            工作描述:&nbsp;\n" +
                  "                            <i class=\"resume_value\">"+lis.getJopDescribes()+"</i>\n" +
                  "                        </div>\n" +
                  "                    </dd>";
               doc.getElementById("work_experiences").append(htm);
            }

         }else {
            doc.getElementById("work_experience").attr("hidden","hidden");
         }



         ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(doc.html().toString().getBytes());
         OutputStream os=response.getOutputStream();
         byte[] b=new byte[1024];
         int length;
         while((length=byteArrayInputStream.read(b))>0){
            os.write(b,0,length);
         }
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}

这是本人自己整合的请多指教

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值