Skip to content

Commit

Permalink
所有 http 上传文件都添加临时文件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
noear committed Mar 6, 2024
1 parent 86f4c96 commit c7073af
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected HttpRequestParser parse() throws Exception {
extension = name.substring(idx + 1);
}

UploadedFile f1 = new UploadedFile(null, contentType, contentSize, content, name, extension);
UploadedFile f1 = new UploadedFile(f0::delete, contentType, contentSize, content, name, extension);

tmp.add(f1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@

import com.sun.net.httpserver.HttpExchange;
import org.noear.solon.boot.ServerProps;
import org.noear.solon.boot.http.HttpPartFile;
import org.noear.solon.boot.jdkhttp.uploadfile.HttpMultipart;
import org.noear.solon.boot.jdkhttp.uploadfile.HttpMultipartCollection;
import org.noear.solon.boot.io.LimitedInputStream;
import org.noear.solon.core.handle.UploadedFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

class MultipartUtil {
public static void buildParamsAndFiles(JdkHttpContext context) throws IOException{
public static void buildParamsAndFiles(JdkHttpContext context) throws IOException {
HttpMultipartCollection parts = new HttpMultipartCollection((HttpExchange) context.request());

while (parts.hasNext()){
while (parts.hasNext()) {
HttpMultipart part = parts.next();
if(isFile(part) == false){
if (isFile(part) == false) {
context.paramSet(part.name, part.getString());
}else{
} else {
doBuildFiles(context, part);
}
}
Expand All @@ -37,36 +35,24 @@ private static void doBuildFiles(JdkHttpContext context, HttpMultipart part) thr
}

String contentType = part.getHeaders().get("Content-Type");
InputStream content = read(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize));
int contentSize = content.available();
HttpPartFile partFile = new HttpPartFile(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize));
String name = part.getFilename();
String extension = null;
int idx = name.lastIndexOf(".");
if (idx > 0) {
extension = name.substring(idx + 1);
}

UploadedFile f1 = new UploadedFile(null, contentType, contentSize, content, name, extension);
UploadedFile f1 = new UploadedFile(partFile::delete, contentType, partFile.getSize(), partFile.getContent(), name, extension);

list.add(f1);
}

private static boolean isField(HttpMultipart filePart){
private static boolean isField(HttpMultipart filePart) {
return filePart.getFilename() == null;
}

private static boolean isFile(HttpMultipart filePart){
private static boolean isFile(HttpMultipart filePart) {
return !isField(filePart);
}

private static ByteArrayInputStream read(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return new ByteArrayInputStream(output.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.noear.jlhttp.HTTPServer;
import org.noear.solon.boot.ServerProps;
import org.noear.solon.boot.http.HttpPartFile;
import org.noear.solon.boot.io.LimitedInputStream;
import org.noear.solon.core.handle.UploadedFile;

Expand Down Expand Up @@ -37,16 +38,15 @@ private static void doBuildFiles(JlHttpContext context, HTTPServer.MultipartIter


String contentType = part.getHeaders().get("Content-Type");
InputStream content = read(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize));
int contentSize = content.available();
HttpPartFile partFile = new HttpPartFile(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize));
String name = part.getFilename();
String extension = null;
int idx = name.lastIndexOf(".");
if (idx > 0) {
extension = name.substring(idx + 1);
}

UploadedFile f1 = new UploadedFile(null, contentType, contentSize, content, name, extension);
UploadedFile f1 = new UploadedFile(partFile::delete, contentType, partFile.getSize(), partFile.getContent(), name, extension);

list.add(f1);
}
Expand All @@ -59,14 +59,4 @@ private static boolean isFile(HTTPServer.MultipartIterator.Part filePart) {
return !isField(filePart);
}

private static ByteArrayInputStream read(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return new ByteArrayInputStream(output.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import org.noear.solon.boot.ServerProps;
import org.noear.solon.boot.http.HttpPartFile;
import org.noear.solon.boot.smarthttp.http.uploadfile.HttpMultipart;
import org.noear.solon.boot.smarthttp.http.uploadfile.HttpMultipartCollection;
import org.noear.solon.boot.io.LimitedInputStream;
Expand Down Expand Up @@ -39,16 +40,15 @@ private static void doBuildFiles(SmHttpContext context, HttpMultipart part) thro
}

String contentType = part.getHeaders().get("Content-Type");
InputStream content = read(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize));
int contentSize = content.available();
HttpPartFile partFile = new HttpPartFile(new LimitedInputStream(part.getBody(), ServerProps.request_maxFileSize));
String name = part.getFilename();
String extension = null;
int idx = name.lastIndexOf(".");
if (idx > 0) {
extension = name.substring(idx + 1);
}

UploadedFile f1 = new UploadedFile(null,contentType, contentSize, content, name, extension);
UploadedFile f1 = new UploadedFile(partFile::delete,contentType, partFile.getSize(), partFile.getContent(), name, extension);

list.add(f1);
}
Expand All @@ -61,14 +61,4 @@ private static boolean isFile(HttpMultipart filePart){
return !isField(filePart);
}

private static ByteArrayInputStream read(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();

byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return new ByteArrayInputStream(output.toByteArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.noear.solon.boot.http;

import org.noear.solon.Utils;
import org.noear.solon.boot.ServerProps;
import org.noear.solon.core.util.IoUtil;

import java.io.*;
import java.nio.file.Files;

/**
* 临时文件
*
* @author noear
* @since 2.7
*/
public class HttpPartFile {
private File tmpfile;
private InputStream inputStream;

public HttpPartFile(InputStream ins) throws IOException {
if (ServerProps.request_useTempfile) {
tmpfile = Files.createTempFile(Utils.guid(), ".tmp").toFile();
try (OutputStream outs = new FileOutputStream(tmpfile)) {
IoUtil.transferTo(ins, outs);
}

inputStream = new FileInputStream(tmpfile);
} else {
ByteArrayOutputStream output = new ByteArrayOutputStream();
IoUtil.transferTo(ins, output);

inputStream = new ByteArrayInputStream(output.toByteArray());
}
}

/**
* 删除
*/
public void delete() throws IOException {
if (tmpfile != null) {
tmpfile.delete();
}
}

/**
* 获取内容
*/
public InputStream getContent() throws IOException {
return inputStream;
}

/**
* 获取大小
*/
public int getSize() throws IOException {
return inputStream.available();
}
}

0 comments on commit c7073af

Please sign in to comment.