java利用joup,imageio,base64来实现简历下载为html格式。
2017-04-26 10:28
295 查看
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> 至\n" + " <i contenteditable=\"true\" class=\"resume_time\">"+new SimpleDateFormat("yyyy.MM.dd").format(lis.getEndTime())+"</i>\n" + " 公司名称:\n" + " <i contenteditable=\"true\" class=\"resume_unit\">"+lis.getEnterpriseName()+"</i>\n" + " 职位:\n" + " <i contenteditable=\"true\" class=\"resume_job\">"+lis.getIndustry()+"</i>\n" + " </div>\n" + " <div >\n" + " 工作描述: \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(); } } }
这是本人自己整合的请多指教
相关文章推荐
- 利用Java 代码创建ftp客户端并实现上传下载等功能
- 把文件以二进制格式或base64字符串形式保存到数据库中,并实现下载功能源码
- java压缩包上传,解压,预览(利用editor.md和Jstree实现)和下载
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- Java 利用Apache Commons Net 实现 FTP文件上传下载
- 利用Java实现Base64加解密
- Java利用apache ftp工具实现文件上传下载和删除功能
- 利用java自带的base64实现加密、解密
- Java 利用Apache Commons Net 实现 FTP文件上传下载
- 利用JS实现常用格式文件下载
- (Java实现)HTML转JPG,TIFF等图片格式和TIFF图片合并功能解决方案。
- 利用 WebClient 实现下载并另存为txt 格式的文本文件
- Java平台要实现类似豆丁百度文科的文档在线阅读,总体思路是讲doc docx等文件格式利用jcom转换成pdf再用swftools转为swf。再用flexpaper组件显示swf。
- 如何用JAVA将二进制文件转换成BASE64格式保存到MySQL的Blob字段里并读出下载
- Java RSA加密解密实现方法分析【附BASE64 jar包下载】
- Java利用SFTP实现文件上传与下载
- 【Java进阶】利用APT优雅的实现统一日志格式
- java 调用jacob和java2word导出word,并实现将带有格式的html文本和图片插入word
- 利用java自带的base64实现加密、解密
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件