首页>代码>springboot使用bootstrap-upload上传文件简单实例,支持多文件上传>/src/main/java/comn/duplicall/upload/controller/UploadController.java
package comn.duplicall.upload.controller; import comn.duplicall.upload.util.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; 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 java.io.File; import java.io.FileOutputStream; import java.io.InputStream; /** * @author liangyafeng * @Description: * @date 2020/4/21 17:01 */ @Controller @Slf4j public class UploadController { //配置文件中配置要上传文件保存的路径 @Value("${filePath}") private String filePath; @RequestMapping(value = "File") public String toFile() { return "FileUpload"; } @RequestMapping(value = "import", method = RequestMethod.POST) public ResponseEntity importFile(@RequestParam("fileUpload") MultipartFile[] fileUpload) { String[] str = new String[fileUpload.length]; //服务器中文件不存在,就创建配置文件中的文件夹 File[] files = new File(filePath).listFiles(); if (files == null) { new File(filePath).mkdirs(); } try { for (int i = 0; i < fileUpload.length; i++) { String fileName = fileUpload[i].getOriginalFilename(); File file = new File(filePath, fileName); InputStream is = fileUpload[i].getInputStream(); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = is.read(bytes)) != -1) { fos.write(bytes, 0, length); } is.close(); fos.close(); str[i] = file.getAbsolutePath(); log.info("upload {} success",fileName); } } catch (Exception e) { e.printStackTrace(); return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity(new Result(null, true), HttpStatus.OK); } }
最近下载更多
longtaolee LV11
4月4日
doutao6677 LV25
2023年6月8日
3159792465 LV10
2023年4月29日
月光skr LV3
2023年4月16日
微信网友_6085661210775552 LV3
2022年11月14日
zhjwgs LV15
2022年3月16日
Niderongyan LV9
2022年3月3日
_NaNa_ LV3
2021年12月10日
lz88888 LV11
2021年10月26日
* * LV2
2021年10月25日
最近浏览更多
1941549176
4月29日
暂无贡献等级
hongdongdong LV12
4月26日
welcome丶 LV8
4月26日
ssh123 LV10
4月23日
longtaolee LV11
4月4日
WBelong LV7
3月29日
内心向阳 LV4
2023年11月8日
张朕朕 LV3
2023年9月10日
lcqlcl LV11
2023年8月29日
泓鼎168 LV19
2023年7月26日