mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-08 06:37:58 +08:00
fix code
This commit is contained in:
parent
942521862d
commit
a967dd6c79
@ -4,12 +4,9 @@ import cn.hutool.core.io.FileUtil;
|
|||||||
import cn.hutool.core.io.IoUtil;
|
import cn.hutool.core.io.IoUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -20,11 +17,12 @@ import java.io.InputStream;
|
|||||||
* @author xiaoleilu
|
* @author xiaoleilu
|
||||||
*/
|
*/
|
||||||
public class UploadFile {
|
public class UploadFile {
|
||||||
|
|
||||||
private static final String TMP_FILE_PREFIX = "hutool-";
|
private static final String TMP_FILE_PREFIX = "hutool-";
|
||||||
private static final String TMP_FILE_SUFFIX = ".upload.tmp";
|
private static final String TMP_FILE_SUFFIX = ".upload.tmp";
|
||||||
|
|
||||||
private UploadFileHeader header;
|
private final UploadFileHeader header;
|
||||||
private UploadSetting setting;
|
private final UploadSetting setting;
|
||||||
|
|
||||||
private int size = -1;
|
private int size = -1;
|
||||||
|
|
||||||
@ -69,7 +67,7 @@ public class UploadFile {
|
|||||||
*/
|
*/
|
||||||
public File write(String destPath) throws IOException {
|
public File write(String destPath) throws IOException {
|
||||||
if (data != null || tempFile != null) {
|
if (data != null || tempFile != null) {
|
||||||
return write(FileUtil.touch(destPath));
|
return write(FileUtil.file(destPath));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -123,10 +121,10 @@ public class UploadFile {
|
|||||||
assertValid();
|
assertValid();
|
||||||
|
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
return new BufferedInputStream(new ByteArrayInputStream(data));
|
return IoUtil.toBuffered(IoUtil.toStream(this.data));
|
||||||
}
|
}
|
||||||
if (tempFile != null) {
|
if (tempFile != null) {
|
||||||
return new BufferedInputStream(new FileInputStream(tempFile));
|
return IoUtil.toBuffered(IoUtil.toStream(this.tempFile));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -190,7 +188,7 @@ public class UploadFile {
|
|||||||
// 处理内存文件
|
// 处理内存文件
|
||||||
int memoryThreshold = setting.memoryThreshold;
|
int memoryThreshold = setting.memoryThreshold;
|
||||||
if (memoryThreshold > 0) {
|
if (memoryThreshold > 0) {
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(memoryThreshold);
|
final ByteArrayOutputStream baos = new ByteArrayOutputStream(memoryThreshold);
|
||||||
int written = input.copy(baos, memoryThreshold);
|
int written = input.copy(baos, memoryThreshold);
|
||||||
data = baos.toByteArray();
|
data = baos.toByteArray();
|
||||||
if (written <= memoryThreshold) {
|
if (written <= memoryThreshold) {
|
||||||
|
@ -3,6 +3,8 @@ package cn.hutool.http.server.action;
|
|||||||
import cn.hutool.http.server.HttpServerRequest;
|
import cn.hutool.http.server.HttpServerRequest;
|
||||||
import cn.hutool.http.server.HttpServerResponse;
|
import cn.hutool.http.server.HttpServerResponse;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 请求处理接口<br>
|
* 请求处理接口<br>
|
||||||
* 当用户请求某个Path,则调用相应Action的doAction方法
|
* 当用户请求某个Path,则调用相应Action的doAction方法
|
||||||
@ -18,5 +20,5 @@ public interface Action {
|
|||||||
* @param request 请求对象
|
* @param request 请求对象
|
||||||
* @param response 响应对象
|
* @param response 响应对象
|
||||||
*/
|
*/
|
||||||
void doAction(HttpServerRequest request, HttpServerResponse response);
|
void doAction(HttpServerRequest request, HttpServerResponse response) throws IOException;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ import cn.hutool.http.server.action.Action;
|
|||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import com.sun.net.httpserver.HttpHandler;
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Action处理器,用于将HttpHandler转换为Action形式
|
* Action处理器,用于将HttpHandler转换为Action形式
|
||||||
*
|
*
|
||||||
@ -26,7 +28,7 @@ public class ActionHandler implements HttpHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(HttpExchange httpExchange) {
|
public void handle(HttpExchange httpExchange) throws IOException {
|
||||||
action.doAction(
|
action.doAction(
|
||||||
new HttpServerRequest(httpExchange),
|
new HttpServerRequest(httpExchange),
|
||||||
new HttpServerResponse(httpExchange)
|
new HttpServerResponse(httpExchange)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.hutool.http.server;
|
package cn.hutool.http.server;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
|
import cn.hutool.core.net.multipart.UploadFile;
|
||||||
import cn.hutool.http.ContentType;
|
import cn.hutool.http.ContentType;
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
|
|
||||||
@ -18,6 +20,16 @@ public class SimpleServerTest {
|
|||||||
.addAction("/formTest", (request, response) ->
|
.addAction("/formTest", (request, response) ->
|
||||||
response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString())
|
response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString())
|
||||||
)
|
)
|
||||||
|
// 文件上传测试
|
||||||
|
// http://localhost:8888/formTest?a=1&a=2&b=3
|
||||||
|
.addAction("/file", (request, response) -> {
|
||||||
|
final UploadFile file = request.getMultipart().getFile("file");
|
||||||
|
// 传入目录,默认读取HTTP头中的文件名然后创建文件
|
||||||
|
file.write("d:/test/");
|
||||||
|
Console.log("Write file to: d:/test/");
|
||||||
|
response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString());
|
||||||
|
}
|
||||||
|
)
|
||||||
.start();
|
.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user