目錄
(一)上傳文件到本地(windows)
(二)上傳文件到linux服務器
(三)跨服務器上傳文件
(一)上傳文件到本地(windows)
1.新建一個文件夾來存儲上傳的文件
在application.yml中寫上傳的文件夾地址
我這里上傳到F盤下的一個文件夾
2.新建controller寫接口
@RestController
public class UploadController {//獲取yml中的文件上傳地址@Value("${windows.path}")private String filePath;@PostMapping("/uploadFile")public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file){//返回map集合Map<String, Object> map = new HashMap<String, Object>();//判斷文件名是否為空if (file.getOriginalFilename() == null){map.put("code",500);map.put("message","文件名為空!");return map;}try {//獲取File文件File file1 = new File(filePath, file.getOriginalFilename());file.transferTo(file1);}catch (Exception e){map.put("code",500);map.put("message","文件服務器上傳失敗!");return map;}map.put("code",200);map.put("message","文件服務器上傳成功!");return map;}
}
? java.io.File類是文件和目錄路徑名稱的抽象表示,主要用于文件和目錄的創建、查找和刪除等操作
我們新建一個File對象,使用里面的
?public File(String parent,String child):從父路徑字符串 和 子路徑字符串創建新的File實例
這個對象會將父路徑字符串與子路徑字符串進行連接并轉換為路徑
然后調用file中的transferto()?方法即可完成上傳
transferto() 方法的作用是將上傳的文件保存到指定的目標位置
3.測試
發起請求
查看文件夾
上傳本地成功!
(二)上傳文件到linux服務器
這個其實也是上傳到本地,因為我們的項目最終要放在linux服務器上運行,在服務器上運行,服務器其實就相當于項目的本地環境
還是剛才的項目
@RestController
public class UploadController {@Value("${file.path}")private String filePath;@PostMapping("/uploadFile")public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file){Map<String, Object> map = new HashMap<String, Object>();if (file.getOriginalFilename() == null){map.put("code",500);map.put("message","文件名為空!");return map;}try {File file1 = new File(filePath, file.getOriginalFilename());//如果目標文件夾不存在就創建if (!file1.exists()){file1.createNewFile();}file.transferTo(file1);}catch (Exception e){map.put("code",500);map.put("message","文件服務器上傳失敗!");return map;}map.put("code",200);map.put("message","文件服務器上傳成功!");return map;}
}
將項目打包部署到服務器上
在pom文件中添加
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.6.7</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skipTests>true</skipTests></configuration></plugin>
</plugins></build>
打包后在項目的target文件夾下找到jar包
上傳到服務器運行
運行成功后訪問接口? ? ? ? 服務器地址+端口+地址(本機訪問必須開放服務器上的項目端口)
查看文件夾
上傳成功!!!
(三)跨服務器上傳文件
前面兩個demo都是本地上傳
這個是訪問本地的項目去上傳到遠程服務器上,例如我有一個專門存儲文件的服務器,我所有的項目都需要將文件存儲到文件服務器上。當我們有多個服務器的時候就可以這樣將所有服務的文件上傳到我們規定的文件服務器中
例如:訪問本地的項目(localhost)將文件上傳到linux服務器上
將上個demo繼續在服務器上運行,然后我們本地的服務去調用剛才那個部署在服務器上的項目的上傳文件的接口
思路:訪問一個服務器上的上傳文件的接口,然后這個接口的方法再調用post請求去訪問文件服務器上的上傳文件的接口就行
繼續新建一個demo項目
依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.21</version></dependency>
server:port: 8080upload:path: http://你的文件服務器地址:9080/uploadFile
@RestController
public class UploadFileController {@Value("${upload.path}")private String uploadPath;@PostMapping("/upload")public String upload(@RequestParam("file") MultipartFile file) throws IOException {//文件為空if (file.isEmpty()){return "文件異常";}CloseableHttpClient client = HttpClients.createDefault();HttpPost httpPost = new HttpPost(uploadPath);HttpEntity httpEntity = MultipartEntityBuilder.create().addBinaryBody("file", file.getBytes(), ContentType.MULTIPART_FORM_DATA, file.getOriginalFilename()).build();httpPost.setEntity(httpEntity);HttpResponse response = client.execute(httpPost);int statusCode = response.getStatusLine().getStatusCode();System.out.println(statusCode);return file.getOriginalFilename();}
}
調用http請求去訪問文件服務器上的上傳文件的接口
測試:
我們訪問本地項目的上傳文件接口
查看linux服務器的文件夾查看是否有這個文件
上傳成功!!!