用java实现multipart/form-data接受请求接口
时间: 2023-07-19 14:03:27 浏览: 237
java map转Multipart/form-data类型body实例
下面是一个使用Java实现multipart/form-data接受请求的示例代码:
```
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.List;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class MultiPartFormHandler implements HttpHandler {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/upload", new MultiPartFormHandler());
server.start();
System.out.println("Server started on port 8000");
}
@Override
public void handle(HttpExchange exchange) throws IOException {
String contentType = exchange.getRequestHeaders().getFirst("Content-Type");
if (contentType != null && contentType.startsWith("multipart/form-data")) {
InputStream in = exchange.getRequestBody();
MultiPartFormData formData = new MultiPartFormData(in, contentType);
List<FormPart> parts = formData.getParts();
for (FormPart part : parts) {
if (part.isFile()) {
saveFile(part);
System.out.println("File saved: " + part.getFileName());
} else {
System.out.println(part.getName() + ": " + part.getText());
}
}
}
exchange.sendResponseHeaders(200, 0);
exchange.close();
}
private void saveFile(FormPart part) throws IOException {
File file = new File(part.getFileName());
OutputStream out = new FileOutputStream(file);
InputStream in = part.getInputStream();
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.close();
in.close();
}
}
class MultiPartFormData {
private List<FormPart> parts;
public MultiPartFormData(InputStream in, String contentType) throws IOException {
String boundary = extractBoundary(contentType);
MultipartStream multipartStream = new MultipartStream(in, boundary.getBytes());
boolean nextPart = multipartStream.skipPreamble();
while (nextPart) {
String header = multipartStream.readHeaders();
FormPart part = new FormPart(header, multipartStream);
parts.add(part);
nextPart = multipartStream.readBoundary();
}
}
private String extractBoundary(String contentType) {
String[] elements = contentType.split(";");
for (String element : elements) {
element = element.trim();
if (element.startsWith("boundary=")) {
return element.substring("boundary=".length());
}
}
return null;
}
public List<FormPart> getParts() {
return parts;
}
}
class FormPart {
private String name;
private String fileName;
private boolean file;
private String text;
private InputStream inputStream;
public FormPart(String header, InputStream inputStream) throws IOException {
String[] elements = header.split(";");
for (String element : elements) {
element = element.trim();
if (element.startsWith("name=")) {
name = element.substring("name=".length());
} else if (element.startsWith("filename=")) {
fileName = element.substring("filename=".length());
file = true;
}
}
if (file) {
this.inputStream = inputStream;
} else {
byte[] buffer = new byte[1024];
int read = inputStream.read(buffer);
text = new String(buffer, 0, read);
}
}
public String getName() {
return name;
}
public String getFileName() {
return fileName;
}
public boolean isFile() {
return file;
}
public String getText() {
return text;
}
public InputStream getInputStream() {
return inputStream;
}
}
```
该示例使用了Java内置的HttpServer和MultipartStream类来处理multipart/form-data请求。HttpServer是一个简单的HTTP服务器,MultipartStream是处理multipart/form-data请求的类。在handle方法中,我们首先检查请求的Content-Type是否是multipart/form-data类型。如果是,我们使用MultipartStream类来解析请求并获取各个部分的内容。对于每个部分,我们检查它是文件还是文本,并执行相应的操作。在这个示例中,我们只是打印文本内容或保存文件。
注意:该示例代码需要Java 8或更高版本才能运行。
阅读全文