Код: Выделить всё
@Path("uploads")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class UploadResource extends BaseResource {
private static final int DEFAULT_BUFFER_SIZE = 8192;
private static final int IMAGE_SIZE_LIMIT = 2000000;
private static final int PDF_SIZE_LIMIT = 5000000;
private static final Logger LOGGER = LoggerFactory.getLogger(UploadResource.class);
private String imageExtension(String type) {
switch (type) {
case "image/jpeg":
return "jpg";
case "image/png":
return "png";
case "image/gif":
return "gif";
case "image/webp":
return "webp";
case "image/svg+xml":
return "svg";
default:
throw new IllegalArgumentException("Unsupported image type");
}
}
@Path("{entity}/{id}")
@POST
@Consumes("image/*")
public Response uploadImage(@PathParam("entity") String entity, @PathParam("id") long entityId, File file,
@HeaderParam(HttpHeaders.CONTENT_TYPE) String type) {
try {
// Extract the file extension from the content type
String extension = type.substring("image/".length());
String name = entityId + "_" + new Date().getTime();
// Ensure correct buffer size and image size limit handling
try (FileInputStream input = new FileInputStream(file);
OutputStream output = Context.getMediaManager().createFileStream(entity, name, extension)) {
byte[] buffer = new byte[8192]; // Use 8 KB buffer
int read;
long transferred = 0;
// Read in chunks and write to output
while ((read = input.read(buffer)) != -1) {
output.write(buffer, 0, read);
transferred += read;
// Check if the image size exceeds the limit
if (transferred > IMAGE_SIZE_LIMIT) {
throw new IllegalArgumentException("Image size limit exceeded"+transferred);
}
}
output.flush(); // Ensure all data is written
}
// Return successful response with the generated image file name
Map result = new HashMap();
result.put("image", name + "." + extension);
return Response.ok(ResponseHelper.getResultWith_200(result)).build();
} catch (Exception ex) {
LOGGER.warn("Image upload failed (LS): ", ex); // Log the entire exception stack trace
return Response.ok(ResponseHelper.getResultWith_400(ResponseHelper.formatMsg(ex.getMessage()))).build();
}
}
}
Код: Выделить всё
public class MediaManager {
private static final Logger LOGGER = LoggerFactory.getLogger(MediaManager.class);
private final String path;
public MediaManager(String path) {
this.path = path;
}
private File createFile(String uniqueId, String name) throws IOException {
Path filePath = Paths.get(path, uniqueId, name);
Path directoryPath = filePath.getParent();
if (directoryPath != null) {
Files.createDirectories(directoryPath);
}
LOGGER.info("Creating file at path: {}", filePath);
return filePath.toFile();
}
public OutputStream createFileStream(String uniqueId, String name, String extension) throws IOException {
File file = createFile(uniqueId, name + "." + extension);
LOGGER.info("Opening output stream for file: {}", file.getAbsolutePath());
return new FileOutputStream(file);
}
}
пустое изображение создается с правильным именем в правильном месте каталог, даже его размер не такой, как 0 КБ, а правильный 502 КБ и так далее
но изображение не работает, невозможно записать его в любом представлении изображения.
пожалуйста, укажите, что я делаю неправильно.
Подробнее здесь: https://stackoverflow.com/questions/791 ... -each-time
Мобильная версия