目錄
一、引言
二、文件的上傳
1、單文件上傳
1.1、數(shù)據(jù)表準備
1.2、添加依賴
1.3、配置文件
1.4、編寫表單
1.5、編寫controller層
2、多文件上傳
2.1、編寫form表單
2.2、編寫controller層
2.3、測試
三、文件下載
四、JREBEL使用
1、下載注冊
2、離線設(shè)置
一、引言
為什么要使用文件的上傳下載?作用?
SpringMVC文件上傳下載是一個常見的功能,它可以讓用戶上傳文件到服務(wù)器或者從服務(wù)器下載文件。這對于許多Web應(yīng)用程序來說是必不可少的功能,比如在線存儲、文檔管理系統(tǒng)等。SpringMVC提供了一些方便的注釋和API,可以使文件上傳和下載變得非常簡單。在文件上傳方面,SpringMVC提供了@RequestParam注釋和MultipartFile類,可以輕松地處理上傳的文件。在文件下載方面,SpringMVC提供了ResponseEntity類,可以將文件作為響應(yīng)發(fā)送給客戶端。
二、文件的上傳
1、單文件上傳
1.1、數(shù)據(jù)表準備
根據(jù)自己的表來也是可以的,只是用來保存數(shù)據(jù)
1.2、添加依賴
在你的spring mvc里面的pom.xml里面添加文件上傳的依賴
<dependencies> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> </dependencies>
1.3、配置文件
在自己的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>
下面我提供我的文件配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--1) 掃描com.tgq及子子孫孫包下的控制器(掃描范圍過大,耗時)--> <context:component-scan base-package="com.tgq"/> <!--2) 此標簽?zāi)J注冊DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> <mvc:annotation-driven/> <!--3) 創(chuàng)建ViewResolver視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- viewClass需要在pom中引入兩個包:standard.jar and jstl.jar --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!--4) 單獨處理圖片、樣式、js等資源 --> <!-- <mvc:resources location="/static/" mapping="/static/**"/>--> <!-- 處理文件上傳下載問題--> <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> <!-- 處理controller層發(fā)送請求到biz,會經(jīng)過切面的攔截處理 --> <aop:aspectj-autoproxy/> </beans>
1.4、編寫表單
表單提交方式為method="post"和enctype="multipart/form-data"
<%--
Created by IntelliJ IDEA.
User: tgq
Date: 9/9/2023
Time: 下午2:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>圖片上傳</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/sc/upload" method="post" enctype="multipart/form-data">
<label>編號:</label><input type="text" name="cid" readonly="readonly" value="${param.cid}"/><br/>
<label>圖片:</label><input type="file" name="zx"/><br/>
<input type="submit" value="上傳圖片"/>
</form>
</body>
</html>
上傳圖片的name名字不能和數(shù)據(jù)庫表名的列名一樣,但是必須要和后端的代碼的名字一樣
1.5、編寫controller層
@Controller
@RequestMapping("/sc")
public class StrutsClasController {
@Autowired
private StrutsClasBiz strutsClasBiz;
/**
* 文件上傳
* <p>
* // * @param req
* // * @param strutsClas
*
* @param zx
* @return
*/
@RequestMapping(value = "/upload")
public String upload(StrutsClas strutsClas, MultipartFile zx) {
// public String upload(HttpServletRequest req, StrutsClas strutsClas, MultipartFile pic) {
try {
//思路:
//1) 將上傳圖片保存到服務(wù)器中的指定位置
// 本地保存地址
// String dir = PropertiesUtil.getValue("dir");
String dir="d:/";
// 網(wǎng)絡(luò)保存地址/upload/
// String server = PropertiesUtil.getValue("server");
String server="/upload/";
// 文件名
String filename = zx.getOriginalFilename();
// System.out.println("文件名:" + filename);
// 文件類別
// System.out.println("文件類別" + zx.getContentType());
System.out.println(strutsClas);
FileUtils.copyInputStreamToFile(zx.getInputStream(), new File(dir + filename));
//2) 更新數(shù)據(jù)庫表t_struts_class圖片記錄
strutsClas.setPic(server + filename);
strutsClasBiz.updateByPrimaryKeySelective(strutsClas);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:list";
}
}
配置tomcat的時候記得添加upload地址映射
2、多文件上傳
2.1、編寫form表單
<form method="post" action="/sc/uploads" enctype="multipart/form-data">
<input type="file" name="files" multiple>
<button type="submit">上傳</button>
</form>
2.2、編寫controller層
/**
* 多文件上傳
*
* @param req
* @param clas
* @param files
* @return
*/
@RequestMapping("/uploads")
public String uploads(HttpServletRequest req,MultipartFile[] files) {
try {
StringBuffer sb = new StringBuffer();
for (MultipartFile cfile : files) {
//思路:
//1) 將上傳圖片保存到服務(wù)器中的指定位置
String dir = "D:/temp/upload/";
String server = "/upload/";
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";
}
2.3、測試
但我們選擇多個文件上傳
我們的本地文件為空
當(dāng)我們上傳之后本地就會進行上傳
運用到我們的數(shù)據(jù)庫也是一樣的
三、文件下載
根據(jù)自己的表來進行操作
<a href="${pageContext.request.contextPath }/sc/download?cid=${b.cid}">下載圖片</a>
編寫編寫controller層方法
/** * 文件下載 * * @param strutsClas * @param req * @return */ @RequestMapping(value = "/download") public ResponseEntity<byte[]> download(StrutsClas strutsClas, HttpServletRequest req) { try { //先根據(jù)文件id查詢對應(yīng)圖片信息 StrutsClas strutsClas1 = this.strutsClasBiz.selectByPrimaryKey(strutsClas.getCid()); //需要下載的地址 String diskPath = PropertiesUtil.getValue("dir"); //服務(wù)器里面保存圖片的地址 String reqPath = PropertiesUtil.getValue("server"); String realPath = strutsClas1.getPic().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; }
當(dāng)我們點擊下載的時候就會進行下載
四、JREBEL使用
1、下載注冊
搜索插件JRebel 并且下載,安裝成功之后會讓你重啟,重啟之后按操作來
在彈出框里面進行注冊
在第一個里面填寫?http://127.0.0.1:8888/GUID ??
GUID:更改為GUID online erstellen? 里面生成的ID填寫
?
最后確認注冊
啟動你的代理。然后運行JRebel
2、離線設(shè)置
進入我們的設(shè)置,前提是我們要開始我們的代理才能進行這個操作文章來源:http://www.zghlxwxcb.cn/news/detail-727056.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-727056.html
到了這里,關(guān)于【SpringMVC】文件上傳與下載、JREBEL使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!