Skip to content

Commit

Permalink
file
Browse files Browse the repository at this point in the history
  • Loading branch information
kongxiangxi committed Oct 13, 2017
1 parent 54d67cc commit c44f70b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.castle.plugin.storage.support;

import java.io.InputStream;
import java.util.List;

import org.springframework.core.io.Resource;
Expand Down Expand Up @@ -52,6 +53,12 @@ public interface FileService {
* @return 路径
*/
String uploadLocal(MultipartFile multipartFile);

String uploadLocal(MultipartFile multipartFile, Visitor visitor);

String uploadLocal(InputStream inputStream, String filename);

String uploadLocal(InputStream inputStream, String filename, Visitor visitor);

/**
* 文件浏览
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,34 @@ public String upload(MultipartFile multipartFile) {

@Override
public String uploadLocal(MultipartFile multipartFile) {
return uploadLocal(multipartFile, null);
}

@Override
public String uploadLocal(InputStream inputStream, String filename) {
return uploadLocal(inputStream, filename, null);
}

@Override
public String uploadLocal(MultipartFile multipartFile, Visitor visitor) {
if (multipartFile == null) {
return null;
}

try {
return uploadLocal(multipartFile.getInputStream(), multipartFile.getOriginalFilename(), visitor);
} catch (IOException e) {
return null;
}
}

@Override
public String uploadLocal(InputStream inputStream, String filename, Visitor visitor) {

String uuid = UUID.randomUUID().toString();
uuid = uuid.replaceAll("-", "");

String path = uuid + "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename());
String path = uuid + "." + FilenameUtils.getExtension(filename);
String destPath = fixUrl(localFileUploadPath, path);

boolean relative = !StringUtils.startsWith(destPath, "/");
Expand All @@ -80,7 +100,11 @@ public String uploadLocal(MultipartFile multipartFile) {
if (!destFile.getParentFile().exists()) {
destFile.getParentFile().mkdirs();
}
Files.write(IOUtils.toByteArray(multipartFile.getInputStream()), destFile);
Files.write(IOUtils.toByteArray(inputStream), destFile);

if (visitor != null) {
visitor.visit(destFile);
}

return fixUrl(relative && Strings.isNullOrEmpty(fileUrl) ? (servletContext.getContextPath() + "/upload") : fileUrl, path);
} catch (IOException e) {
Expand Down Expand Up @@ -247,4 +271,5 @@ private void httpDownloadFile(String url, String filePath) {
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.castle.plugin.storage.support;

import java.io.File;

public interface Visitor {

void visit(File file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ header .uk-navbar-item, .uk-navbar-nav>li>a, .uk-navbar-toggle {
.parsley-errors-list {
margin: 0;
color: red;
float: left;
}

.select-cell {
Expand Down

0 comments on commit c44f70b

Please sign in to comment.