Skip to content

Commit

Permalink
test-fileServer-新加文件get方法单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Ning19230223 committed Oct 29, 2024
1 parent 8f119c0 commit 5fa4def
Showing 1 changed file with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.charset.StandardCharsets;

@ApplicationScoped
public class FileServer {

final Logger logger = LoggerFactory.getLogger(getClass());

String originalFileName;

@Inject
FileServerConfig config;

@Inject
Vertx vertx;

@Inject
IFileService fileService;

Expand Down Expand Up @@ -82,10 +84,11 @@ private void upload(RoutingContext ctx) {
for (FileUpload f : ctx.fileUploads()) {
String uploadedFileName = f.uploadedFileName();
originalFileName = f.fileName();
originalFileName = StringEscapeUtils.unescapeHtml4(originalFileName);
File file = new File(uploadedFileName);
String id = fileService.save(file, originalFileName);
ctx.response()
// .putHeader("Content-Type", "text/plain;charset=utf-8")
.putHeader("Content-Type", "text/plain;charset=utf-8")
.write(id);
}
ctx.response()
Expand All @@ -106,10 +109,11 @@ private void download(RoutingContext ctx) {
vertx.fileSystem().readFile(nameFilePath, result -> {
if (result.succeeded()) {
Buffer buffer = result.result();
String content = buffer.toString("UTF-8");

if (fileObtained.exists()) {
ctx.response()
.putHeader("Content-Disposition", "attachment; filename=" + content)
.putHeader("Content-Disposition", "attachment; filename=" + new String(buffer.getBytes(), StandardCharsets.ISO_8859_1))
.putHeader("Content-type", "application/octet-stream")
.sendFile(fileObtained.getPath());
// vertx.fileSystem().delete(fileObtained.getPath());
fileObtained.deleteOnExit();
Expand Down Expand Up @@ -144,5 +148,5 @@ private void info(RoutingContext ctx) {
ctx.fail(err);
});
}

}

0 comments on commit 5fa4def

Please sign in to comment.