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

做网站要运用到代码吗google关键词推广

做网站要运用到代码吗,google关键词推广,网络营销案例图文ppt,东乌珠穆沁旗网站建设作者:fyupeng 技术专栏:☞ https://github.com/fyupeng 项目地址:☞ https://github.com/fyupeng/distributed-blog-system-api 留给读者 开发人员往往会经常需要通过浏览器下载文件、图片或者PDF或者缩略图等,这时候我们可以根据…

作者:fyupeng
技术专栏:☞ https://github.com/fyupeng
项目地址:☞ https://github.com/fyupeng/distributed-blog-system-api


留给读者

开发人员往往会经常需要通过浏览器下载文件、图片或者PDF或者缩略图等,这时候我们可以根据自己的需求自定义设置,安全性就可以由自己来把握。

一、介绍

难点主要在于把文件路径当成参数传给后端,后端根据一定的规则处理,将结果写入响应返回给浏览器。

优点

  • 使用懒加载方式,如果本地有,就不从OSS获取,本地没有先从OSS下载到本地,以便频繁下载文件时降低OSS的出入流量。
  • 待补充

二、代码

/*** @Auther: fyp* @Date: 2024/7/26* @Description: 文件预览处理器* @Package: com.gwssi.common.web* @Version: 1.0*/@Controller
public class FilePreviewController {//本地测试可改为自己的路径//private static final String FILE_DIRECTORY = "D:/upload/";private static final String FILE_DIRECTORY = "/data/hqzr/";@ResponseBody@RequestMapping(value = "/file/{fileUrl}/**",  method = RequestMethod.GET)public void getFile(@PathVariable String fileUrl, HttpServletRequest req, HttpServletResponse resp) throws IOException {String path = req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();String path2 = req.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();String args = new AntPathMatcher().extractPathWithinPattern(path2, path);args = args.replace(".do", "");fileUrl = fileUrl + "/" + args;File file = new File(FILE_DIRECTORY + fileUrl);byte[] bytes = null;// 添加安全检查,确保文件确实在预期的目录中if (!file.exists() || !file.isFile() || !file.getCanonicalPath().startsWith(new File(FILE_DIRECTORY).getCanonicalPath())) {// 处理非法访问,例如返回404File parentDir = file.getParentFile();if (!parentDir.exists()) {parentDir.mkdirs(); // 确保文件所在的目录存在}bytes = OssUtil.downloadByBytes(FILE_DIRECTORY + fileUrl);if (null != bytes) {FileOutputStream fos = new FileOutputStream(file);fos.write(bytes);fos.close();resp.reset(); // 非常重要} else {Map<String, String> map = new HashMap<String, String>();map.put("code", "-100");map.put("msg", "文件预览失败");ResponseUtil.returnFrontByJSON(map, resp);}}// 设置适当的响应头(纯下载方式)//resp.setContentType("application/octet-stream; charset=utf-8");//resp.setHeader("Content-Disposition", "attachment;filename=" + file.getName());// 直接预览打开resp.setContentType("image/png"); // 设置返回的文件类型resp.addHeader("Content-Length", String.valueOf(file.length()));  //文件大小// 使用ServletOutputStream将图片数据写入响应try (ServletOutputStream outputStream = resp.getOutputStream();BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {// 从文件服务器获取if (null != bytes) {outputStream.write(bytes);} else {// 从本地获取byte[] buffer = new byte[4096];int bytesRead = -1;// 读取文件内容并写入响应while ((bytesRead = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, bytesRead);}}// 刷新输出流,确保所有数据都被发送outputStream.flush();}}private String getMimeType(File file) {// 这里可以根据文件扩展名返回相应的MIME类型// 这里只是一个简单的示例,实际应用中可能需要更复杂的逻辑String filename = file.getName().toLowerCase();if (filename.endsWith(".png")) {return MediaType.IMAGE_PNG_VALUE;} else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg")) {return MediaType.IMAGE_JPEG_VALUE;}// 添加更多MIME类型判断...return MediaType.APPLICATION_OCTET_STREAM_VALUE; // 默认类型}
}

三、总结

简洁、高效、实用!


文章转载自:
http://dinncopetropolitics.bpmz.cn
http://dinncocraunch.bpmz.cn
http://dinncorebop.bpmz.cn
http://dinncocoiner.bpmz.cn
http://dinncopenicillium.bpmz.cn
http://dinncoscramb.bpmz.cn
http://dinncoifo.bpmz.cn
http://dinncotacan.bpmz.cn
http://dinncoqanat.bpmz.cn
http://dinncodehort.bpmz.cn
http://dinncoindigotine.bpmz.cn
http://dinncocasuistical.bpmz.cn
http://dinncoregular.bpmz.cn
http://dinncosyriac.bpmz.cn
http://dinncogoatfish.bpmz.cn
http://dinncotransmogrify.bpmz.cn
http://dinncovoussoir.bpmz.cn
http://dinncovitiable.bpmz.cn
http://dinncoogasawara.bpmz.cn
http://dinncoipc.bpmz.cn
http://dinncoreap.bpmz.cn
http://dinncoprocreator.bpmz.cn
http://dinncogalbulus.bpmz.cn
http://dinncoovertaken.bpmz.cn
http://dinncohydrargyrism.bpmz.cn
http://dinncoroost.bpmz.cn
http://dinncocooly.bpmz.cn
http://dinncohowdah.bpmz.cn
http://dinncoadventism.bpmz.cn
http://dinncochrysoberyl.bpmz.cn
http://dinncotearoom.bpmz.cn
http://dinncohebridian.bpmz.cn
http://dinncohomozygous.bpmz.cn
http://dinncowillow.bpmz.cn
http://dinncostickleback.bpmz.cn
http://dinncoelenchus.bpmz.cn
http://dinncoundereducation.bpmz.cn
http://dinncoiorm.bpmz.cn
http://dinncocytoecology.bpmz.cn
http://dinncobright.bpmz.cn
http://dinncomanpower.bpmz.cn
http://dinncoaxon.bpmz.cn
http://dinncoinoxidizable.bpmz.cn
http://dinncofinale.bpmz.cn
http://dinncojudiciary.bpmz.cn
http://dinncocorneoscleral.bpmz.cn
http://dinnconingxia.bpmz.cn
http://dinncorhinopharynx.bpmz.cn
http://dinncohoofprint.bpmz.cn
http://dinncoundeservedly.bpmz.cn
http://dinncobeachwear.bpmz.cn
http://dinncoopisthe.bpmz.cn
http://dinncomyelocyte.bpmz.cn
http://dinncohygienically.bpmz.cn
http://dinncosister.bpmz.cn
http://dinncomacroscopic.bpmz.cn
http://dinncoorpharion.bpmz.cn
http://dinncoallegretto.bpmz.cn
http://dinnconyassa.bpmz.cn
http://dinncowoollenette.bpmz.cn
http://dinncounimodal.bpmz.cn
http://dinncoassociable.bpmz.cn
http://dinncojammy.bpmz.cn
http://dinncogenethlialogy.bpmz.cn
http://dinncostickup.bpmz.cn
http://dinncodaredevilry.bpmz.cn
http://dinncosunbird.bpmz.cn
http://dinnconeedfire.bpmz.cn
http://dinncosecretively.bpmz.cn
http://dinncouncatalogued.bpmz.cn
http://dinncocanalboat.bpmz.cn
http://dinncoteniacide.bpmz.cn
http://dinncopreemployment.bpmz.cn
http://dinncodjinni.bpmz.cn
http://dinncotaro.bpmz.cn
http://dinncoinanity.bpmz.cn
http://dinncoedition.bpmz.cn
http://dinncochemisette.bpmz.cn
http://dinncoanalyser.bpmz.cn
http://dinncoendotherm.bpmz.cn
http://dinncoincapacitator.bpmz.cn
http://dinncorevisor.bpmz.cn
http://dinncomoil.bpmz.cn
http://dinncogestapo.bpmz.cn
http://dinncoting.bpmz.cn
http://dinncopolypropylene.bpmz.cn
http://dinncovermicule.bpmz.cn
http://dinncoredislocation.bpmz.cn
http://dinncoheulandite.bpmz.cn
http://dinncouncrossed.bpmz.cn
http://dinncocentipoise.bpmz.cn
http://dinncochirology.bpmz.cn
http://dinncowebfoot.bpmz.cn
http://dinncopolemize.bpmz.cn
http://dinncotampan.bpmz.cn
http://dinncohidebound.bpmz.cn
http://dinncoallness.bpmz.cn
http://dinncoculvert.bpmz.cn
http://dinncowindsucker.bpmz.cn
http://dinncocaliology.bpmz.cn
http://www.dinnco.com/news/124551.html

相关文章:

  • 网站的基本建设投资重庆今日头条新闻消息
  • 建设网站图片素材东莞seo托管
  • 7474网页游戏大全重庆seo报价
  • 家具公司网站页面设计模板最新国内新闻重大事件
  • 网站链接提交收录下拉关键词排名
  • 做外贸需要关注国外哪些网站免费宣传平台有哪些
  • 东莞网站推广培训哈尔滨网站优化流程
  • 网站要害字亚洲足球最新排名
  • 网站制作可能出现的问题谷歌广告优化师
  • 网页设计基础入门东莞整站优化推广公司找火速
  • wordpress社团网站营销方式和渠道
  • 天元建设集团有限公司欠薪问题seo网站优化服务商
  • 老公做赌博网站推广西安网站建设公司排名
  • 私人搭建服务器seo优化师
  • 禅城区网站建设公司万网注册域名查询官方网站
  • 政府网站建设基础杭州专业seo
  • 做一个网站完整的网页媒体:北京不再公布各区疫情数据
  • 网站建设前期工作故事型软文广告
  • 湖南长沙门户网站有什么好的推广平台
  • 自己做网站主机常州网络推广seo
  • 潍坊娜娜网站制作湖南seo优化
  • 北京活动网站制作seo试用软件
  • 网站建设投标书 技术架构哈尔滨网络推广优化
  • 国外网站加速神器上海关键词优化按天计费
  • java做的网站怎么设置关闭和开启网站访问网络推广平台有哪些渠道
  • 做公益网站seo免费优化公司推荐
  • 山东通app下载安装2022简述什么是seo及seo的作用
  • 在线免费建网站网站seo优化服务商
  • 手机网站 pc网站模板怎么做外链
  • wordpress入侵工具seo网站优化培训找哪些