-
大小: 1KB文件類型: .zip金幣: 2下載: 0 次發布日期: 2021-05-14
- 語言: Java
- 標簽: springboot??
資源簡介
springboot 寫的微信小程序后臺上傳 包含了文件上傳 和下載 由于文件是在整個后臺中拿出來的,所以返回參數 可以根據自己需求進行修改。文件主要是在微信小程序 生成錄音文件 或選擇文件時 調用 wx.upload 方法上傳文件的后臺支持。

代碼片段和文件信息
package?hongbao.web.controller;
import?hongbao.autoconfig.F4jProperties;
import?hongbao.autoconfig.FileProperties;
import?hongbao.common.FileTypeEnum;
import?hongbao.controller.baseController;
import?hongbao.entity.HongBaoFile;
import?hongbao.file.FileStore;
import?hongbao.model.JsonResult;
import?hongbao.model.ResultUtil;
import?hongbao.model.VoiceStoreDto;
import?hongbao.service.FileServiceI;
import?io.swagger.annotations.Api;
import?io.swagger.annotations.ApiOperation;
import?org.springframework.beans.factory.annotation.Autowired;
import?org.springframework.boot.context.properties.EnableConfigurationProperties;
import?org.springframework.http.HttpRequest;
import?org.springframework.web.bind.annotation.*;
import?org.springframework.web.multipart.MultipartFile;
import?javax.servlet.http.HttpServletResponse;
import?javax.validation.Valid;
import?java.io.*;
import?java.net.HttpURLConnection;
import?java.net.URL;
import?java.util.Date;
@CrossOrigin
@RestController
@RequestMapping(“/file“)
public?class?FileController?extends?baseController?{
????@Autowired
????private?FileServiceI?fileService;
????@RequestMapping(value?=?“/uploadPicFile“?method?=?{RequestMethod.POST})
????public?JsonResult?filePicUpload(@RequestParam(value?=?“file“)?MultipartFile?file)?throws?IOException?{
????????String?path=?“文件存儲路徑“;
????????String?fileName=file.getOriginalFilename();
?????????if?(!file.isEmpty())?{
?????????????try?{
?????????????????File?fileDir?=?new?File(path);
?????????????????if?(!fileDir.exists())?{//?判斷目錄是否存在
?????????????????????fileDir.mkdirs();
?????????????????????//dir.mkdirs();??//多層目錄需要調用mkdirs
?????????????????}
?????????????????byte[]?fdate?=?file.getBytes();
?????????????????FileOutputStream?out?=?new?FileOutputStream(path+fileName);
?????????????????out.write(fdate);
?????????????????out.close();
?????????????????//保存文件
?????????????????/*
????這里寫文件業務邏輯
?*/
?????????????????return?ResultUtil.success(bFile);
?????????????}?catch?(IOException?e)?{
?????????????????e.printStackTrace();
?????????????????return?ResultUtil.error(410?“操作失敗“);
?????????????}
????????}else?{
????????????return?ResultUtil.error(410?“文件不能為空“);
????????}
????}
????@RequestMapping(value?=?“/fileDownload“?method?=?RequestMethod.GET)
????public?void?fileDownload(@RequestParam(value?=?“fileId“)?String?fileIdHttpServletResponse?res)?{
????????//獲取文件
????????HongBaoFile?bFile?=?fileService.selectById(fileId);
????????res.setHeader(“content-type“?“application/octet-stream“);
????????res.setContentType(“application/octet-stream“);
????????res.setHeader(“Content-Disposition“?“attachment;filename=“?+?bFile.getFileName());
????????byte[]?buff?=?new?byte[1024];
????????BufferedInputStream?bis?=?null;
????????OutputStream?os?=?null;
????????try?{
????????????os?=?res.getOutputStream();
????????????bis?=?new?BufferedInputStream(new?FileInputStream(new?File(bFile.getFilePath()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????3574??2018-02-13?20:34??FileController.java
評論
共有 條評論