此種方式上傳文件相對簡單,以下均經測試成功,才提供到此。
以下為單個文件上傳方式
分析:本次的工作目的是根據一級標題產生對應的二級標題,在每個二級標題下對應一個(file字段)新聞文件,當點擊新聞文件的時候顯示新聞文件內容,所以需要上傳新聞文件。很多表單中都會有文件上傳這一項,將文件上傳到數據庫 可以理解為將文件路徑上傳到數據庫,而真正的文件儲存在IDEA編譯運行之后產生的(target/項目名稱/指明的目錄名) 下(也可以儲存在服務器上如Tomact),另外,本次實現屬於單個文件實現,在上傳時單獨給文件賦值別稱。以下是實現步驟:
使用工具:IDEA MySQL 框架:Spring、Springmvc 、Mybatis
1、基礎條件為根據一級標題的Id查詢到對應的二級標題全部信息,接收二級標題信息的是一個List集合,泛型為二級標題對應的實體類。注意一點:需要在二級標題中創建一級標題的實體對象。
2、導入jar包(我使用的是maven倉庫),並且需要在pom.xml、springmvc.xml中配置
Pom.xml配置
(1)定義依賴版本號
<properties>
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<commons-io.version>1.3.2</commons-io.version>
</properties>
(2)配置組件
<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
</dependencies>
Springmvc.xml對象創建配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設置默認編碼 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上傳圖片最大大小5M-->
<property name="maxUploadSize" value="5242440"></property>
</bean>
3、接下來是代碼實現
JSP頁面實現(其他普通屬性不在贅述):
(1)<form name="form1" method="post"
action="請求Controller路徑" enctype="multipart/form-data">
注意:method="post"為表單請求方式,優點是安全並且不限制大小,紅色部分表示以二進制碼形式傳輸數據,用於上傳文件,必須填寫。
下面是表單中<input標簽>
<input type="file" name="file1">
(2)Controller頁面實現
package com.jin.controller;
import com.jin.domain.Secondleveltitle;
import com.jin.service.SecondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
@Controller
@RequestMapping("/second")
public class SecondController {
@Autowired
private SecondService secondservice;
//二級標題發布
@RequestMapping(value = "/secondsave",method = RequestMethod.POST)
public String secondsave(@RequestParam(value = "file1") MultipartFile file, HttpServletRequest request, Secondleveltitle second) {
try {
//上傳后的地址,注意("/upload")是表示文件上傳后的目標文件夾
String realPath = request.getSession().getServletContext().getRealPath("/upload");
System.out.println("打印文件上傳的路徑"+realPath);
//獲取文件名
String filename = file.getOriginalFilename();
//獲取文件后綴名
String extensionname = filename.substring(filename.lastIndexOf(".") + 1);
//給上傳的文件起別名,有很多種方式
String newFilename=String.valueOf(System.currentTimeMillis())+"."+extensionname;
//創建File對象,傳入目標路徑參數,和新的文件別名
File dir=new File(realPath,newFilename);
if (!dir.exists()){//如果dir代表的文件不存在,則創建它,
dir.mkdirs();//
}
//如果存在則直接執行下面操作
file.transferTo(dir);//將上傳的實體文件復制到指定目錄upload下
second.setFile(newFilename);//將文件名賦值給實體類,然后一並存到數據庫
secondservice.secondSave(second);//調用后台方法,將對象存進數據庫
System.out.println("測試是否執行完畢");
} catch (IOException e) {
e.printStackTrace();
}
return "title_file_sucess";//執行完畢,返回一個邏輯視圖
}
}
4、最后一步非常重要,需要在Tomact服務器中找到設置的upload文件夾,即配置虛擬目錄
點擊中心綠色+號,選擇External Source..
找到生成的upload文件夾,我的在程序的target目錄下找到
在Application context中填寫文件夾名如 /upload
以上測試即可。
成功截圖:數據庫文件名稱
Upload文件夾中的文件實體
文件打開展示
異常產生已改進點:一個file類型異常,無法賦值。原因是在實體類中有個String 類型的file是儲存文件名用,在JSP表單中有
此name並非是和實體對象一致,其type類型是 file。所以,兩個名字不要一樣。
轉載請注明出處