首页>代码>springboot+sqlite3+iceEditor开发网页版记事本>/myMark/src/main/resources/static/js/iceUI/editor/upload/java-upload.java
/* 依赖: <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.14</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> */ package com.slpchina.site.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.apache.http.entity.ContentType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import net.sf.json.JSONArray; import net.sf.json.JSONObject; @Controller public class FileController { //改成你的文件存储路径,结尾别忘加斜杠 private String filePath = "C:/Users/"; //文件上传 @RequestMapping(value="/uploadFile") @ResponseBody public JSONArray uploadFile(@RequestParam(value="file[]")MultipartFile file[]) throws IOException { FileInputStream fileInputStream = null; //创建JSON数组,返回一组url和name,告诉编辑器上传的图片路径 JSONArray jsonArray = new JSONArray(); try { //循环为编辑器传来的图片改名 for(int i=0;i<file.length;i++) { String uuid = UUID.randomUUID().toString().replaceAll("-",""); //获取文件后缀名,并拼接UUID和后缀名 String suffix = file[i].getOriginalFilename().substring(file[i].getOriginalFilename().lastIndexOf(".")); File newFile = new File(uuid + suffix); FileUtils.copyInputStreamToFile(file[i].getInputStream(), newFile); fileInputStream = new FileInputStream(newFile); file[i] = new MockMultipartFile(newFile.getName(), newFile.getName(),ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); //调用fileService的上传方法 upload(file[i]); //创建JSON对象并加入JSON数组 JSONObject jsonObject = new JSONObject(); jsonObject.put("url", "/file/" + file[i].getOriginalFilename()); jsonObject.put("name", file[i].getOriginalFilename()); jsonObject.put("error", 0); jsonArray.add(jsonObject); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return jsonArray; } public void upload(MultipartFile upload) { String fileName = upload.getOriginalFilename(); File file = new File(filePath); if(!file.exists()) file.mkdirs(); String newFilePath=filePath+fileName; try { upload.transferTo(new File(newFilePath)); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } } }

7214978 LV2
1月7日
hanweinan6 LV13
2024年10月9日
Seaskye LV14
2023年11月4日
笑得很美 LV11
2023年10月12日
DeanYao LV2
2023年10月11日
1483259138 LV1
2023年9月18日
最代码官方 LV168
2023年9月16日

blanknb
3月18日
暂无贡献等级
段池卿 LV1
1月19日
7214978 LV2
1月7日
yimaoermao LV1
2024年11月28日
charleswang LV7
2024年10月21日
hanweinan6 LV13
2024年10月9日
Peny_ZH LV5
2024年9月20日
develop LV10
2024年6月2日
hmf1989
2024年4月30日
暂无贡献等级
kenhomeliu LV29
2024年4月30日