保定网站seo哪家公司好,网站需要去工信部做备案,搭建三合一网站,石家庄网站推广方案目录
一、JRebel
1.1 Jrebel介绍
1.2 Jrebel插件下载
1.3 Jrebel服务下载并启动
1.4 在线生成GUID
1.5 JRebel激活
1.6 相关设置
注意❗ 二、文件上传、下载
2.1 导入pom依赖
2.2 配置文件上传解析器
2.3 文件上传表单设置
2.4 文件上传实现
2.5 文件下载实现
2…目录
一、JRebel
1.1 Jrebel介绍
1.2 Jrebel插件下载
1.3 Jrebel服务下载并启动
1.4 在线生成GUID
1.5 JRebel激活
1.6 相关设置
注意❗ 二、文件上传、下载
2.1 导入pom依赖
2.2 配置文件上传解析器
2.3 文件上传表单设置
2.4 文件上传实现
2.5 文件下载实现
2.6 多文件上传、下载
注意❗ 一、JRebel
1.1 Jrebel介绍 JRebel是一个Java开发工具它是一款用于实时代码重载的插件。它的主要功能是在不重新启动应用程序的情况下将修改后的Java代码实时应用到正在运行的应用程序中从而加快开发周期提高开发效率。 实时代码重载JRebel可以监测开发人员对Java代码的修改并将这些修改实时应用到正在运行的应用程序中无需重新启动应用程序。这样开发人员可以立即看到他们的代码更改的效果节省了重新编译和部署的时间。 支持多种框架和服务器JRebel支持多种Java框架和服务器包括Spring、Hibernate、Java EE、Tomcat、Jetty等。无论是开发Web应用程序还是企业级应用程序JRebel都可以与常用的开发框架和服务器集成提供实时代码重载的功能。 高度兼容性JRebel与大多数Java开发工具和集成开发环境IDE兼容如Eclipse、IntelliJ IDEA、NetBeans等。开发人员可以在他们喜欢的IDE中使用JRebel插件无需切换到其他工具。 快速部署JRebel可以加快应用程序的部署速度因为它只需要将修改后的代码应用到运行中的应用程序而不需要重新启动整个应用程序。这对于大型应用程序和复杂的部署环境特别有用。 支持多种开发场景JRebel可以应用于各种开发场景包括本地开发、远程开发、云开发等。无论是单机开发还是分布式开发JRebel都可以提供实时代码重载的功能。 总的来说JRebel是一个强大的Java开发工具它通过实时代码重载的功能使开发人员能够在不重新启动应用程序的情况下快速应用他们对Java代码的修改。这大大提高了开发效率减少了开发周期使开发人员能够更专注于代码的编写和调试。 1.2 Jrebel插件下载 打开IDEA选择File—Settings—Plugins—在搜索框输入jrebel 1.3 Jrebel服务下载并启动
首先下载服务进入GitHub网址Release v1.4 · ilanyu/ReverseProxy · GitHub 下载到指定位置后双击启动即可如下图 启动后就可以打开idea进行激活如不打开服务则会出现如下错误 1.4 在线生成GUID 在线生成GUID网址GUID online erstellen
如果失效刷新GUID替换就可以 1.5 JRebel激活 服务器地址https://127.0.0.1:8888/{GUID} 输入服务器地址将{GUID}替换成上面的字符串注意要除去{}然后再输入邮箱只需正确的邮箱格式即可。 最后成功的界面 安装成功之后就可以通过JRebel启动项目。这样修改完Java代码后就可以不再需要重启服务器这样浪费时间的操作了。 1.6 相关设置
1. 设置成离线工作模式
设置成离线模式后就不需要启动 ReverseProxy_windows_amd64 服务了 点击设置成离线模式后如下图 2. 设置自动编译
要想实现热部署首先需要对Intellij按如下进行设置 由于JRebel是实时监控class文件的变化来实现热部署的所以在idea环境下需要打开自动变异功能才能实现随时修改随时生效。 注意事项❗ 通过Jrebel启动项目之前需要打开Jrebel服务不然就会出错正确启动项目后不要把服务关闭先关服务器tomcat再关掉Jrebel服务。如需使用离线模式也不能先关jrebel服务设置好、关掉tomcat最后才关Jrebel服务之后就再也不需要开启Jrebel服务就可以直接通过Jrebel启动项目了。 二、文件上传、下载
2.1 导入pom依赖 commons-fileupload.version1.3.3/commons-fileupload.versiondependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion${commons-fileupload.version}/version/dependency 2.2 配置文件上传解析器
在spring-mvc.xml文件中添加文件上传解析器。
bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver!-- 必须和用户JSP 的pageEncoding属性一致以便正确解析表单的内容 --property namedefaultEncoding valueUTF-8/property!-- 文件最大大小(字节) 1024*1024*5050M--property namemaxUploadSize value52428800/property!--resolveLazily属性启用是为了推迟文件解析以便捕获文件大小异常--property nameresolveLazily valuetrue/
/bean 这段代码配置了一个名为multipartResolver的Bean用于处理文件上传。通过设置defaultEncoding属性、maxUploadSize属性和resolveLazily属性可以指定文件上传时的字符编码、最大上传大小和延迟文件解析的行为。这样Spring框架在处理文件上传时会根据这些配置进行相应的解析和限制。 2.3 文件上传表单设置 定义一个简单的文件上传表单页面设置enctype属性指定了表单数据的编码类型为multipart/form-data这是用于支持文件上传的编码类型。然后设置上传的文件将会被命名为imgFile用于被后台接受最后将文件上传到指定的URL。
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headbase href${pageContext.request.contextPath }title文件上传/title
/head
body
form action/file/upload methodpost enctypemultipart/form-datalabel编号/labelinput typetext nameid readonlyreadonly value${param.id}/br/label图片/labelinput typefile nameimgFile/br/input typesubmit value上传图片/
/form
/body
/html2.4 文件上传实现
步骤一设计数据表 步骤二配置表信息并生成代码
generatorConfig.xml : table schema tableNameimg_upload domainObjectNameUploadImgenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/table
步骤三创建业务逻辑层并实现接口...
步骤四配置文件路径信息
resource.properties
#本地路径
dirD:/upload/
#服务器路径
server/upload/
编写配置文件读取工具类
package com.ycxw.utils;import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;/*** 配置文件读取工具类* author 云村小威* site blog.csdn.net/Justw320* create 2023-09-10 16:57*/
public class PropertiesUtil {public static String getValue(String key) throws IOException {Properties p new Properties();InputStream in PropertiesUtil.class.getResourceAsStream(/resource.properties);p.load(in);return p.getProperty(key);}
}步骤五配置项目与映射地址 步骤六编写控制器
package com.ycxw.web;import com.ycxw.biz.UploadImgBiz;
import com.ycxw.model.UploadImg;
import com.ycxw.utils.PageBean;
import com.ycxw.utils.PropertiesUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-09-10 16:50*/
Controller
RequestMapping(/file)
public class UploadImgController {Autowiredprivate UploadImgBiz uploadImgBiz;/*新增方法*/RequestMapping(/add)public String save(UploadImg uploadImg, HttpServletRequest request) {uploadImgBiz.insertSelective(uploadImg);return redirect:list;}/*删除方法*/RequestMapping(/del/{id})public String del(PathVariable(id) Integer id) {uploadImgBiz.deleteByPrimaryKey(id);return redirect:/file/list;}/*修改方法*/RequestMapping(/edit)public String edit(UploadImg uploadImg, HttpServletRequest request) {uploadImgBiz.updateByPrimaryKeySelective(uploadImg);return redirect:list;}/*查询方法*/RequestMapping(/list)public String list(UploadImg uploadImg, HttpServletRequest request) {PageBean pageBean new PageBean();pageBean.setRequest(request);ListUploadImg uploadImgs uploadImgBiz.listPager(uploadImg, pageBean);
// ModelAndView modelAndView new ModelAndView();
// modelAndView.addObject(UploadImgs, UploadImgs);
// modelAndView.addObject(pageBean, pageBean);
// modelAndView.setViewName(UploadImg/list);request.setAttribute(uploadImgs, uploadImgs);request.setAttribute(pageBean, pageBean);return file/list;}/*数据回显*/RequestMapping(/preSave)public String preSave(UploadImg uploadImg, HttpServletRequest request) {if (uploadImg ! null uploadImg.getId() ! null uploadImg.getId() ! 0) {UploadImg img uploadImgBiz.selectByPrimaryKey(uploadImg.getId());request.setAttribute(img, img);}return file/edit;}/*图片上传*/RequestMapping(upload)public String upload(UploadImg img,MultipartFile imgFile) throws IOException {//读取配置文夹本地路径和服务器路径String dir PropertiesUtil.getValue(dir);String server PropertiesUtil.getValue(server);//利用MultipartFile类接受前端传递到后台的文件System.out.println(文件名imgFile.getOriginalFilename());System.out.println(文件类型imgFile.getContentType());//将文件转成流写入到服务器FileUtils.copyInputStreamToFile(imgFile.getInputStream(),new File(dirimgFile.getOriginalFilename()));//通过对象将图片保存到数据库img.setImg(serverimgFile.getOriginalFilename());uploadImgBiz.updateByPrimaryKeySelective(img);return redirect:list;}
}步骤七编写前端jsp
% page contentTypetext/html;charsetUTF-8 languagejava %
% taglib prefixc urihttp://java.sun.com/jsp/jstl/core %
% taglib prefixw urihttp://jsp.veryedu.cn %
html
headmeta http-equivContent-Type contenttext/html; charsetUTF-8linkhrefhttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.cssrelstylesheetscriptsrchttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js/scriptbase href${pageContext.request.contextPath }title博客列表/titlestyle typetext/css.page-item input {padding: 0;width: 40px;height: 100%;text-align: center;margin: 0 6px;}.page-item input, .page-item b {line-height: 38px;float: left;font-weight: 400;}.page-item.go-input {margin: 0 10px;}/style
/head
body
form classform-inlineaction/file/list methodpostdiv classform-group mb-2input typetext classform-control-plaintext namenameplaceholder请输入用户名称/divbutton typesubmit classbtn btn-primary mb-2查询/buttona classbtn btn-primary mb-2 href/file/preSave新增/a
/formtable classtable table-stripedtheadtrth scopecolID/thth scopecol用户/thth scopecol图片/th/tr/theadtbodyc:forEach vari items${uploadImgs }trtd${i.id }/tdtd${i.name }/tdtdimg src${i.img } stylewidth: 200px;height: 100px;/tdtda href/file/preSave?id${i.id}修改/aa href/file/del/${i.id}删除/aa href/page/file/upload?id${i.id}图片上传/aa href/file/download?id${i.id}图片下载/a/td/tr/c:forEach/tbody
/table
!-- 这一行代码就相当于前面分页需求前端的几十行了 --
w:page pageBean${pageBean }/w:page/body
/html 步骤8运行测试 2.5 文件下载实现 根据传入的文件id查询对应的文件信息然后根据文件路径读取文件内容并将文件内容和设置好的HTTP头信息封装成一个ResponseEntity对象最后返回给客户端进行文件下载。 RequestMapping(/download)public ResponseEntitybyte[] download(UploadImg uploadImg, HttpServletRequest req){try {//先根据文件id查询对应图片信息UploadImg img this.uploadImgBiz.selectByPrimaryKey(uploadImg.getId());String diskPath PropertiesUtil.getValue(dir);String reqPath PropertiesUtil.getValue(server);//上面获取的数据库地址需要转换才能下载成本地路径String realPath img.getImg().replace(reqPath,diskPath);String fileName realPath.substring(realPath.lastIndexOf(/)1);//下载关键代码File filenew File(realPath);HttpHeaders headers new HttpHeaders();//http头信息String downloadFileName new String(fileName.getBytes(UTF-8),iso-8859-1);//设置编码headers.setContentDispositionFormData(attachment, downloadFileName);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//MediaType:互联网媒介类型 contentType具体请求中的媒体类型信息return new ResponseEntitybyte[](FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);}catch (Exception e){e.printStackTrace();}return null;} 示例 2.6 多文件上传、下载
1. 编写方法 RequestMapping(/download)public ResponseEntitybyte[] download(UploadImg uploadImg, HttpServletRequest req) {try {//先根据文件id查询对应图片信息UploadImg img this.uploadImgBiz.selectByPrimaryKey(uploadImg.getId());String diskPath PropertiesUtil.getValue(dir);String reqPath PropertiesUtil.getValue(server);//上面获取的数据库地址需要转换才能下载成本地路径String realPath img.getImg().replace(reqPath, diskPath);String fileName realPath.substring(realPath.lastIndexOf(/) 1);//下载关键代码File file new File(realPath);HttpHeaders headers new HttpHeaders();//http头信息String downloadFileName new String(fileName.getBytes(UTF-8), iso-8859-1);//设置编码headers.setContentDispositionFormData(attachment, downloadFileName);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//MediaType:互联网媒介类型 contentType具体请求中的媒体类型信息return new ResponseEntitybyte[](FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);} catch (Exception e) {e.printStackTrace();}return null;} 2. 编写前端页面做对比
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headbase href${pageContext.request.contextPath }title文件上传/title
/head
body
h3单文件上传/h3
form action/file/upload methodpost enctypemultipart/form-datalabel编号/labelinput typetext nameid readonlyreadonly value${param.id}/br/label图片/labelinput typefile nameimgFile/br/input typesubmit value上传图片/
/form
br
h3多文件上传/h3
form methodpost action/file/uploads enctypemultipart/form-datainput typefile namefiles multiplebutton typesubmit上传/button
/form
/body
/html3. 运行测试 如下图多文件上传选择了10个文件而单文件上传功能使用ctrl选择是无效的只能选择当个文件。 接下测试多文件上传是否能在本地查看 注意事项❗ 选择多张图片的时候需要注意设置文件上传的大小还有数据库存储图片的字段的类型长度是否超过这都会影响文件是否能上传成功。因为多张图片的空间累计需要更多的空间通过避免图片名称过长查看存储该字段的长度是否能装的下。