首页>代码>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(); } } }
最近下载更多
Seaskye LV14
2023年11月4日
笑得很美 LV11
2023年10月12日
DeanYao LV2
2023年10月11日
1483259138 LV1
2023年9月18日
最代码官方 LV167
2023年9月16日
最近浏览更多
hmf1989
4月30日
暂无贡献等级
kenhomeliu LV29
4月30日
微信网友_6957774974095360 LV1
4月19日
lo8lukoukoum LV4
4月11日
玖零定制问题修复 LV34
4月4日
阿时23428 LV15
3月23日
chenlie LV2
3月18日
wanglinddad LV55
3月13日
quartz LV7
3月13日
Iterman LV2
3月12日