Commit 9cc02676 authored by 陈精华's avatar 陈精华 Committed by kl

优化:允许上传多个同一类型文件

parent a21f35c2
...@@ -7,6 +7,8 @@ import com.google.common.collect.ImmutableMap; ...@@ -7,6 +7,8 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.utils.FileUtils; import cn.keking.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
...@@ -28,15 +30,17 @@ import java.util.UUID; ...@@ -28,15 +30,17 @@ import java.util.UUID;
*/ */
@RestController @RestController
public class FileController { public class FileController {
String fileDir = ConfigConstants.getFileDir();
private final Logger logger = LoggerFactory.getLogger(FileController.class);
private String fileDir = ConfigConstants.getFileDir();
@Autowired @Autowired
FileUtils fileUtils; private FileUtils fileUtils;
String demoDir = "demo"; private String demoDir = "demo";
String demoPath = demoDir + File.separator; private String demoPath = demoDir + File.separator;
@RequestMapping(value = "fileUpload", method = RequestMethod.POST) @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
public String fileUpload(@RequestParam("file") MultipartFile file, public String fileUpload(@RequestParam("file") MultipartFile file) throws JsonProcessingException {
HttpServletRequest request) throws JsonProcessingException {
// 获取文件名 // 获取文件名
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
//判断是否为IE浏览器的文件名,IE浏览器下文件名会带有盘符信息 //判断是否为IE浏览器的文件名,IE浏览器下文件名会带有盘符信息
...@@ -49,15 +53,15 @@ public class FileController { ...@@ -49,15 +53,15 @@ public class FileController {
if (pos != -1) { if (pos != -1) {
fileName = fileName.substring(pos + 1); fileName = fileName.substring(pos + 1);
} }
// 判断是否存在同名文件
// 判断该文件类型是否有上传过,如果上传过则提示不允许再次上传 if (existsFile(fileName)) {
if (existsTypeFile(fileName)) { return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(1, "存在同名文件,请先删除原有文件再次上传", null));
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(1, "每一种类型只可以上传一个文件,请先删除原有文件再次上传", null));
} }
File outFile = new File(fileDir + demoPath); File outFile = new File(fileDir + demoPath);
if (!outFile.exists()) { if (!outFile.exists()) {
outFile.mkdirs(); outFile.mkdirs();
} }
logger.info("上传文件:{}", outFile.getAbsolutePath());
try(InputStream in = file.getInputStream(); try(InputStream in = file.getInputStream();
OutputStream ot = new FileOutputStream(fileDir + demoPath + fileName)){ OutputStream ot = new FileOutputStream(fileDir + demoPath + fileName)){
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
...@@ -67,7 +71,7 @@ public class FileController { ...@@ -67,7 +71,7 @@ public class FileController {
} }
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null)); return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); logger.error("文件上传失败", e);
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(1, "FAILURE", null)); return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(1, "FAILURE", null));
} }
} }
...@@ -78,6 +82,7 @@ public class FileController { ...@@ -78,6 +82,7 @@ public class FileController {
fileName = fileName.substring(fileName.lastIndexOf("/") + 1); fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
} }
File file = new File(fileDir + demoPath + fileName); File file = new File(fileDir + demoPath + fileName);
logger.info("删除文件:{}", file.getAbsolutePath());
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} }
...@@ -106,18 +111,11 @@ public class FileController { ...@@ -106,18 +111,11 @@ public class FileController {
* @return * @return
* @param fileName * @param fileName
*/ */
private boolean existsTypeFile(String fileName) { private boolean existsFile(String fileName) {
boolean result = false; boolean result = false;
String suffix = fileUtils.getSuffixFromFileName(fileName); File file = new File(fileDir + demoPath + fileName);
File file = new File(fileDir + demoPath);
if (file.exists()) { if (file.exists()) {
for(File file1 : file.listFiles()){ return true;
String existsFileSuffix = fileUtils.getSuffixFromFileName(file1.getName());
if (suffix.equals(existsFileSuffix)) {
result = true;
break;
}
}
} }
return result; return result;
} }
......
...@@ -212,12 +212,15 @@ window.open('http://127.0.0.1:8012/picturesPreview?urls='+encodeURIComponent(fil ...@@ -212,12 +212,15 @@ window.open('http://127.0.0.1:8012/picturesPreview?urls='+encodeURIComponent(fil
// 上传完成,刷新table // 上传完成,刷新table
if (1 == data.code) { if (1 == data.code) {
alert(data.msg); alert(data.msg);
}else{ } else {
$('#table').bootstrapTable('refresh', {}); $('#table').bootstrapTable('refresh', {});
} }
$(".loading_container").hide(); $(".loading_container").hide();
}, },
error: function (error) { alert(error); $(".loading_container").hide();}, error: function () {
alert('上传失败,请联系管理员');
$(".loading_container").hide();
},
url: 'fileUpload', /*设置post提交到的页面*/ url: 'fileUpload', /*设置post提交到的页面*/
type: "post", /*设置表单以post方法提交*/ type: "post", /*设置表单以post方法提交*/
dataType: "json" /*设置返回值类型为文本*/ dataType: "json" /*设置返回值类型为文本*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment