Commit 25611a95 by Patryk Czarnik

Upload zdjęć

parent e8a306e9
......@@ -2,11 +2,10 @@ package sklep.photo;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
......@@ -39,11 +38,11 @@ public class PhotoUtil {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot read photo for product id = " + productId);
}
}
public void writeStream(int productId, InputStream inputStream) {
public void writeStream(int productId, byte[] bytes) {
try {
Path path = getPath(productId);
Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
Files.write(path, bytes, StandardOpenOption.CREATE);
} catch (Exception e) {
// wypisujemy błąd, ale metoda kończy się normalnie
e.printStackTrace();
......
package sklep.rest;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
......@@ -150,5 +151,9 @@ public class ProductController {
public byte[] getPhoto(@PathVariable("id") int productId) {
return photoUtil.readBytes(productId);
}
@PutMapping(path="/{id}/photo", consumes="image/jpeg")
public void uploadPhoto(@PathVariable("id") int productId, @RequestBody byte[] bytes) {
photoUtil.writeStream(productId, bytes);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment