forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploadService.java
More file actions
67 lines (48 loc) · 1.83 KB
/
FileUploadService.java
File metadata and controls
67 lines (48 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.zetcode.service;
import com.zetcode.form.FileUploadForm;
import org.jboss.logging.Logger;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Paths;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.WRITE;
@Path("upload")
public class FileUploadService {
Logger log = Logger.getLogger(FileUploadService.class);
@POST
@Consumes("multipart/form-data")
public Response uploadFile(@MultipartForm FileUploadForm form) {
var data = form.getData();
var fileName = form.getFileName();
if (data.length == 0 || fileName.isBlank()) {
return Response.status(Response.Status.BAD_REQUEST)
.entity("form not filled correctly").build();
}
var isWritten = writeFile(form.getData(), fileName);
if (isWritten) {
var msg = String.format("File %s uploaded", fileName);
return Response.status(200).entity(msg).build();
} else {
var msg = String.format("Failed to upload %s file", fileName);
return Response.status(500).entity(msg).build();
}
}
private boolean writeFile(byte[] content, String fileName) {
var dirName = "C:/Users/Jano/Documents/tmp";
var filePath = String.format("%s/%s", dirName, fileName);
OpenOption[] options = {CREATE, WRITE};
try {
Files.write(Paths.get(filePath), content, options);
} catch (IOException e) {
log.error("failed to save file", e);
return false;
}
return true;
}
}