优化命名

This commit is contained in:
redkale
2023-11-10 22:42:40 +08:00
parent 03152e4e6a
commit 0391304024
3 changed files with 27 additions and 11 deletions

View File

@@ -17,7 +17,7 @@ import java.util.*;
*/ */
public class MimeType { public class MimeType {
private final static Map<String, String> contentTypes = new HashMap<>(); private static final Map<String, String> contentTypes = new HashMap<>();
static { static {
contentTypes.put("abs", "audio/x-mpeg"); contentTypes.put("abs", "audio/x-mpeg");
@@ -177,6 +177,9 @@ public class MimeType {
contentTypes.put("zip", "application/zip"); contentTypes.put("zip", "application/zip");
} }
private MimeType() {
}
public static String get(String extension) { public static String get(String extension) {
return contentTypes.getOrDefault(extension.toLowerCase(), "text/plain"); return contentTypes.getOrDefault(extension.toLowerCase(), "text/plain");
} }
@@ -198,7 +201,9 @@ public class MimeType {
public static String getByFilename(String fileName) { public static String getByFilename(String fileName) {
int length = fileName.length(); int length = fileName.length();
int newEnd = fileName.lastIndexOf('#'); int newEnd = fileName.lastIndexOf('#');
if (newEnd == -1) newEnd = length; if (newEnd == -1) {
newEnd = length;
}
int i = fileName.lastIndexOf('.', newEnd); int i = fileName.lastIndexOf('.', newEnd);
return (i < 0) ? null : get(fileName.substring(i + 1, newEnd).toLowerCase()); return (i < 0) ? null : get(fileName.substring(i + 1, newEnd).toLowerCase());
} }

View File

@@ -108,7 +108,7 @@ public final class MultiContext {
continue;//不遍历完后面getParameter可能获取不到值 continue;//不遍历完后面getParameter可能获取不到值
} }
has = true; has = true;
if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFilename().matches(fileNameRegx)) { if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFileName().matches(fileNameRegx)) {
continue; continue;
} }
if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) { if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) {
@@ -157,13 +157,13 @@ public final class MultiContext {
continue; //不遍历完后面getParameter可能获取不到值 continue; //不遍历完后面getParameter可能获取不到值
} }
has = true; has = true;
if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFilename().matches(fileNameRegx)) { if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFileName().matches(fileNameRegx)) {
continue; continue;
} }
if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) { if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) {
continue; continue;
} }
File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFilename()); File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFileName());
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.isDirectory()) { if (!parent.isDirectory()) {
parent.mkdirs(); parent.mkdirs();
@@ -197,13 +197,13 @@ public final class MultiContext {
} }
List<File> files = null; List<File> files = null;
for (MultiPart part : parts()) { for (MultiPart part : parts()) {
if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFilename().matches(fileNameRegx)) { if (fileNameRegx != null && !fileNameRegx.isEmpty() && !part.getFileName().matches(fileNameRegx)) {
continue; continue;
} }
if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) { if (contentTypeRegx != null && !contentTypeRegx.isEmpty() && !part.getContentType().matches(contentTypeRegx)) {
continue; continue;
} }
File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFilename()); File file = new File(home, "tmp/redkale-" + System.nanoTime() + "_" + part.getFileName());
File parent = file.getParentFile(); File parent = file.getParentFile();
if (!parent.isDirectory()) { if (!parent.isDirectory()) {
parent.mkdirs(); parent.mkdirs();

View File

@@ -17,7 +17,7 @@ import java.util.concurrent.atomic.LongAdder;
*/ */
public final class MultiPart { public final class MultiPart {
private final String filename; private final String fileName;
private final String name; private final String name;
@@ -28,7 +28,7 @@ public final class MultiPart {
private final LongAdder received; private final LongAdder received;
MultiPart(String fileName, String name, String contentType, LongAdder received, InputStream in) { MultiPart(String fileName, String name, String contentType, LongAdder received, InputStream in) {
this.filename = fileName; this.fileName = fileName;
this.name = name; this.name = name;
this.in = in; this.in = in;
this.contentType = contentType; this.contentType = contentType;
@@ -37,7 +37,7 @@ public final class MultiPart {
@Override @Override
public String toString() { public String toString() {
return this.getClass().getSimpleName() + "{" + "name=" + name + ", filename=" + filename + ", contentType=" + contentType + ", received=" + received + '}'; return this.getClass().getSimpleName() + "{" + "name=" + name + ", fileName=" + fileName + ", contentType=" + contentType + ", received=" + received + '}';
} }
public boolean save(File file) throws IOException { public boolean save(File file) throws IOException {
@@ -99,8 +99,19 @@ public final class MultiPart {
return contentType; return contentType;
} }
public String getFileName() {
return fileName;
}
/**
* @see #getFileName()
*
* @return String
* @deprecated replace by {@link #getFileName() }
*/
@Deprecated(since = "2.8.0")
public String getFilename() { public String getFilename() {
return filename; return fileName;
} }
public String getName() { public String getName() {