idea安裝jerbel及文件上傳下載的實現(xiàn)示例
一、JRebel
1.1 Jrebel介紹
JRebel是一個Java開發(fā)工具,它是一款用于實時代碼重載的插件。它的主要功能是在不重新啟動應(yīng)用程序的情況下,將修改后的Java代碼實時應(yīng)用到正在運行的應(yīng)用程序中,從而加快開發(fā)周期,提高開發(fā)效率。
- 實時代碼重載:JRebel可以監(jiān)測開發(fā)人員對Java代碼的修改,并將這些修改實時應(yīng)用到正在運行的應(yīng)用程序中,無需重新啟動應(yīng)用程序。這樣,開發(fā)人員可以立即看到他們的代碼更改的效果,節(jié)省了重新編譯和部署的時間。
- 支持多種框架和服務(wù)器:JRebel支持多種Java框架和服務(wù)器,包括Spring、Hibernate、Java EE、Tomcat、Jetty等。無論是開發(fā)Web應(yīng)用程序還是企業(yè)級應(yīng)用程序,JRebel都可以與常用的開發(fā)框架和服務(wù)器集成,提供實時代碼重載的功能。
- 高度兼容性:JRebel與大多數(shù)Java開發(fā)工具和集成開發(fā)環(huán)境(IDE)兼容,如Eclipse、IntelliJ IDEA、NetBeans等。開發(fā)人員可以在他們喜歡的IDE中使用JRebel插件,無需切換到其他工具。
- 快速部署:JRebel可以加快應(yīng)用程序的部署速度,因為它只需要將修改后的代碼應(yīng)用到運行中的應(yīng)用程序,而不需要重新啟動整個應(yīng)用程序。這對于大型應(yīng)用程序和復(fù)雜的部署環(huán)境特別有用。
- 支持多種開發(fā)場景:JRebel可以應(yīng)用于各種開發(fā)場景,包括本地開發(fā)、遠程開發(fā)、云開發(fā)等。無論是單機開發(fā)還是分布式開發(fā),JRebel都可以提供實時代碼重載的功能。
總的來說,JRebel是一個強大的Java開發(fā)工具,它通過實時代碼重載的功能,使開發(fā)人員能夠在不重新啟動應(yīng)用程序的情況下,快速應(yīng)用他們對Java代碼的修改。這大大提高了開發(fā)效率,減少了開發(fā)周期,使開發(fā)人員能夠更專注于代碼的編寫和調(diào)試。
1.2 Jrebel插件下載
首先我們要到idea的插件商店中下載jerbel插件
然后進入gitup的開源網(wǎng)站,下載jerbel
Release v1.4 · ilanyu/ReverseProxy · GitHub
我用的是windows的就下載windows and64就行
下載到指定位置以后,雙擊啟動服務(wù)
出現(xiàn)這個就是啟動成功了
然后我們進入idea,激活jerbel
點擊之后,會彈出激活界面
第一欄填入
http://localhost:8888/
后面接上GUID碼,這里也提供一個給大家,Create GUID online (guidgen.com)
第二欄是自己的電子郵箱
值得注意的是,我在填第一欄的地址的時候,使用127.0.0.1不可以,但是使用localhost就可以了,知道的大佬可以評論區(qū)指點指點
激活成功之后,我們就可以jerbel啟動了
1.3 jerbel離線設(shè)置
因為我們每一次使用erble都需要啟動服務(wù)太過麻煩,所以此時我們需要設(shè)置離線模式
我這里是因為我已經(jīng)開啟了,沒開啟只有一個按鈕,是off開頭的,點擊開啟就好了
開啟之后我們再次啟動jerble就不需要再次啟動服務(wù)了
值得注意的是,我們在關(guān)閉程序之前,需要先關(guān)閉tomcat,再關(guān)閉服務(wù),這樣離線使用時,我們就不需要再去開啟jerbel了
二、文件的上傳和下載操作
2.1 導(dǎo)入pom依賴
<commons-fileupload.version>1.3.3</commons-fileupload.version> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency>
2.2 配置文件上傳解析器
在spring-mvc.xml文件中添加文件上傳解析器。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 必須和用戶JSP 的pageEncoding屬性一致,以便正確解析表單的內(nèi)容 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 文件最大大小(字節(jié)) 1024*1024*50=50M--> <property name="maxUploadSize" value="52428800"></property> <!--resolveLazily屬性啟用是為了推遲文件解析,以便捕獲文件大小異常--> <property name="resolveLazily" value="true"/> </bean>
這段代碼配置了一個名為"multipartResolver"的Bean,用于處理文件上傳。通過設(shè)置"defaultEncoding"屬性、"maxUploadSize"屬性和"resolveLazily"屬性,可以指定文件上傳時的字符編碼、最大上傳大小和延遲文件解析的行為。這樣,Spring框架在處理文件上傳時會根據(jù)這些配置進行相應(yīng)的解析和限制。
2.3、指定文件上傳的表單
文件上傳的表單指定enctype屬性為multipart/form-data
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <base href="${pageContext.request.contextPath }" rel="external nofollow" rel="external nofollow" > <title>文件上傳</title> </head> <body> <form action="/file/upload" method="post" enctype="multipart/form-data"> <label>編號:</label><input type="text" name="id" readonly="readonly" value="${param.id}"/><br/> <label>圖片:</label><input type="file" name="imgFile"/><br/> <input type="submit" value="上傳圖片"/> </form> </body> </html>
2.4、設(shè)置項目的映射地址
2.5、編寫控制器
package com.liwen.web; import com.liwen.biz.UploadImgBiz; import com.liwen.model.UploadImg; import com.liwen.utils.PageBean; import com.liwen.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 { @Autowired private 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); List<UploadImg> 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"; } /*數(shù)據(jù)回顯*/ @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 { //讀取配置文夾本地路徑和服務(wù)器路徑 String dir = PropertiesUtil.getValue("dir"); String server = PropertiesUtil.getValue("server"); //利用MultipartFile類接受前端傳遞到后臺的文件 System.out.println("文件名:"+imgFile.getOriginalFilename()); System.out.println("文件類型:"+imgFile.getContentType()); //將文件轉(zhuǎn)成流寫入到服務(wù)器 FileUtils.copyInputStreamToFile(imgFile.getInputStream(),new File(dir+imgFile.getOriginalFilename())); //通過對象將圖片保存到數(shù)據(jù)庫 img.setImg(server+imgFile.getOriginalFilename()); uploadImgBiz.updateByPrimaryKeySelective(img); return "redirect:list"; } /*文件下載*/ @RequestMapping("/download") public ResponseEntity<byte[]> download(UploadImg uploadImg, HttpServletRequest req){ try { //先根據(jù)文件id查詢對應(yīng)圖片信息 UploadImg img = this.uploadImgBiz.selectByPrimaryKey(uploadImg.getId()); String diskPath = PropertiesUtil.getValue("dir"); String reqPath = PropertiesUtil.getValue("server"); //上面獲取的數(shù)據(jù)庫地址,需要轉(zhuǎn)換才能下載成本地路徑 String realPath = img.getImg().replace(reqPath,diskPath); String fileName = realPath.substring(realPath.lastIndexOf("/")+1); //下載關(guān)鍵代碼 File file=new File(realPath); HttpHeaders headers = new HttpHeaders();//http頭信息 String downloadFileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");//設(shè)置編碼 headers.setContentDispositionFormData("attachment", downloadFileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //MediaType:互聯(lián)網(wǎng)媒介類型 contentType:具體請求中的媒體類型信息 return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.OK); }catch (Exception e){ e.printStackTrace(); } return null; } }
2.6、jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="w" uri="http://jsp.veryedu.cn" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="external nofollow" rel="stylesheet"> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script> <base href="${pageContext.request.contextPath }" rel="external nofollow" rel="external nofollow" > <title>博客列表</title> <style type="text/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 class="form-inline" action="/file/list" method="post"> <div class="form-group mb-2"> <input type="text" class="form-control-plaintext" name="name" placeholder="請輸入用戶名稱"> </div> <button type="submit" class="btn btn-primary mb-2">查詢</button> <a class="btn btn-primary mb-2" href="/file/preSave" rel="external nofollow" >新增</a> </form> <table class="table table-striped"> <thead> <tr> <th scope="col">ID</th> <th scope="col">用戶</th> <th scope="col">圖片</th> </tr> </thead> <tbody> <c:forEach var="i" items="${uploadImgs }"> <tr> <td>${i.id }</td> <td>${i.name }</td> <td> <img src="${i.img }" style="width: 200px;height: 100px;"> </td> <td> <a href="/file/preSave?id=${i.id}" rel="external nofollow" >修改</a> <a href="/file/del/${i.id}" rel="external nofollow" >刪除</a> <a href="/page/file/upload?id=${i.id}" rel="external nofollow" >圖片上傳</a> <a href="/file/download?id=${i.id}" rel="external nofollow" >圖片下載</a> </td> </tr> </c:forEach> </tbody> </table> <!-- 這一行代碼就相當于前面分頁需求前端的幾十行了 --> <w:page pageBean="${pageBean }"></w:page> </body> </html>
三、多文件上傳
3.1、控制層
//多文件上傳 @RequestMapping("/uploads") public String uploads(HttpServletRequest req, Student student, MultipartFile[] files){ try { StringBuffer sb = new StringBuffer(); for (MultipartFile cfile : files) { //思路: //1) 將上傳圖片保存到服務(wù)器中的指定位置 String dir = PropertiesUtil.getValue("dir"); String server = PropertiesUtil.getValue("server"); String filename = cfile.getOriginalFilename(); FileUtils.copyInputStreamToFile(cfile.getInputStream(),new File(dir+filename)); sb.append(filename).append(","); } System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } return "redirect:list"; }
3.2、jsp
<a href="${pageContext.request.contextPath }/page/student/uploads" rel="external nofollow" >多文件上傳</a> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath }/student/uploads" method="post" enctype="multipart/form-data"> <input type="file" name="files" multiple> <button type="submit">上傳</button> </form> </body> </html>
到此這篇關(guān)于idea安裝jerbel及文件上傳下載的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)idea安裝jerbel及文件上傳下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Retrofit+Rxjava實現(xiàn)帶進度顯示的下載文件
這篇文章主要為大家詳細介紹了基于Retrofit+Rxjava實現(xiàn)帶進度顯示的下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
這篇文章主要介紹了Spring Cloud Gateway Hystrix fallback獲取異常信息的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java數(shù)據(jù)結(jié)構(gòu)之ArrayList從順序表到實現(xiàn)
Java中的ArrayList是一種基于數(shù)組實現(xiàn)的數(shù)據(jù)結(jié)構(gòu),支持動態(tài)擴容和隨機訪問元素,可用于實現(xiàn)順序表等數(shù)據(jù)結(jié)構(gòu)。ArrayList在內(nèi)存中連續(xù)存儲元素,支持快速的隨機訪問和遍歷。通過學(xué)習(xí)ArrayList的實現(xiàn)原理和使用方法,可以更好地掌握Java中的數(shù)據(jù)結(jié)構(gòu)和算法2023-04-04使用sts工具、SpringBoot整合mybatis的詳細步驟
這篇文章主要介紹了使用sts工具、SpringBoot整合mybatis的詳細步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04