当前位置: 首页 > news >正文

做网站怎么去工信部缴费北京优化推广

做网站怎么去工信部缴费,北京优化推广,wordpress老是有人注册,资讯门户类网站Sptingboot项目 — maven打包,云效,docker,k8s 场景 — 导出excel模板 问题 1.乱码 2.下载为0KB,打开没有数据 模板内容 测试代码 测试方法 方法过程结果问题原因将文件直接放到服务器使用接口下载数据正常,排除文件问题排…

Sptingboot项目
— maven打包,云效,docker,k8s
场景
— 导出excel模板
问题
1.乱码
在这里插入图片描述

2.下载为0KB,打开没有数据
在这里插入图片描述
模板内容
在这里插入图片描述

测试代码

测试方法

方法过程结果问题原因
将文件直接放到服务器使用接口下载数据正常,排除文件问题排除接口问题,文件问题
文件放到resource目录下使用接口下载下载乱码使用相同接口下载乱码,猜测是maven编译问题
文件移出到resouce目录外使用接口下载下载为0KB使用相同接口下载乱码,猜测是maven编译问题

代码

InputStream inputStream = null;ServletOutputStream servletOutputStream = null;try {//注意修改目录Resource resource = new DefaultResourceLoader().getResource("classpath:file/goods_import.xls");response.setContentType("application/force-download");response.setHeader("Content-Disposition", "attachment;fileName=" + new String("goods_import".getBytes(), StandardCharsets.ISO_8859_1)+ ".xls");inputStream = resource.getInputStream();servletOutputStream = response.getOutputStream();IOUtils.copy(inputStream, servletOutputStream);response.flushBuffer();} catch (Exception e) {log.error("下载批量上传用户模板文件错误", e);} finally {try {if (servletOutputStream != null) {servletOutputStream.close();}if (inputStream != null) {inputStream.close();}} catch (Exception e) {log.error("下载批量上传用户模板文件错误", e);}}

过程-可以跳过直接看解决办法
— 遵循网上的办法,使用字节流,设置字节大小,设置编码,都测试过一遍。

设置字节大小
   		InputStream inputStream = getClass().getClassLoader().getResourceAsStream("classpath:file/goods_import.xls");File file = new File("goods_import.xls"); // 指定下载文件的路径和名称try (OutputStream outputStream = new FileOutputStream(file)) {// 将inputStream中的数据写入到outputStream中,‌确保文件不为0KBbyte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}} catch (IOException e) {e.printStackTrace(); // 处理异常}finally {if (file.exists()){file.delete();}}

设置字符集编码

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");response.setHeader("Content-Disposition", "attachment; filename=\"" + "file/goods_import.xls" + "\"");//这里设置为GBK,UTF-8都不行response.setCharacterEncoding("GBK");try (BufferedInputStream inputStream = new BufferedInputStream(ImportBusiness.class.getClassLoader().getResourceAsStream("file/goods_import.xls"));OutputStream outputStream = response.getOutputStream()) {if (inputStream == null) {// 处理文件未找到的情况response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found");return;}byte[] buffer = new byte[1024];int bytesRead;while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}outputStream.flush();} catch (IOException e) {// 处理IO异常response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "IO error");}

使用字节流输出

/*** 模板下载*/public void downloadTemplate(HttpServletResponse response) {OutputStream out = null;InputStream in = null;ByteArrayOutputStream bos = null;String fileName = "good_import";try {// 读取模板Resource res = new ClassPathResource("classpath:file/goods_import.xls");XSSFWorkbook workbook = new XSSFWorkbook(res.getInputStream());// 转换为字节流bos = new ByteArrayOutputStream();workbook.write(bos);byte[] barray = bos.toByteArray();in = new ByteArrayInputStream(barray);response.reset();response.setContentType("application/octet-stream");response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");out = response.getOutputStream();byte[] b = new byte[1024];int len;while ((len = in.read(b)) > 0) {out.write(b, 0, len);}out.flush();} catch (Exception e) {log.error("下载模板失败", e);} finally {if (null != in) {try {in.close();} catch (IOException e) {log.error("关闭资源异常", e);}}if (null != out) {try {out.close();} catch (IOException e) {log.error("关闭资源异常", e);}}if (null != bos) {try {bos.flush();bos.close();} catch (IOException e) {log.error("关闭资源异常", e);}}}}

经测试以上方法均不行,然后考虑到jar打包会被编译,会不会是这个导致的呢。能不能忽略掉这个xls文件呢?

这里使用maven打包,使用提供的插件排除,如下

			<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.2.0</version><configuration><nonFilteredFileExtensions><nonFilteredFileExtension>xlsx</nonFilteredFileExtension><nonFilteredFileExtension>xls</nonFilteredFileExtension></nonFilteredFileExtensions></configuration></plugin>

如图框中区域

重新调用接口下载,文件正常

http://www.mmbaike.com/news/27187.html

相关文章:

  • wordpress电影网站模板安徽企业网站建设
  • 做展厅 参考什么网站宁波seo关键词优化方法
  • 杭州商城网站制作如何获取永久免费域名
  • 做背景图 网站google play下载安装
  • 都什么网站用wordpress游戏推广代理加盟
  • 做视频直播网站微信推广方式有哪些
  • 广告公司网站首页湖南网站设计外包费用
  • 两学一做网上答题网站关键词规划师工具
  • iis5.1 建立网站百度引流推广
  • 网站的连接二维码怎么做湖北seo
  • 吉林省建设银行网站杭州关键词优化测试
  • 做公司网站有没有必要唐山seo
  • 搭建网站需要的软件百度推广的效果
  • 做网站首页psseo工作
  • 企业网站维护费用白山网络推广
  • 大连网站建设怎么做怎样制作网站
  • 零基础网站建设教学培训班网站推广步骤
  • 精能集团有限公司两学一做网站培训seo去哪家机构最好
  • 优惠券网站建设制作网站开发公司排名
  • 清河县做网站seo技术自学
  • 网站建设与维护公司seo信息是什么
  • 九江市网站建设阿里云建站
  • 东明网站建设武汉百度百科
  • 社交网站怎么做今日热搜榜排行榜
  • 制作网站对话框关于手机的软文营销
  • 南宁网站优化推广方案网络营销的作用
  • 企业网站建设论文网络营销与直播电商好就业吗
  • 互联网开发工资一般多少淘宝优化关键词的步骤
  • 做网站代理工作安全吗廊坊百度推广seo
  • 如何提升网站转化率网络推广的平台有哪些