Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230617
Commits
763ad912
Commit
763ad912
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PhotoUtil jako Component i @Value
parent
5a0ee948
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
16 additions
and
9 deletions
+16
-9
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+4
-1
PhotoUtil.java
PC27-SklepSpring/src/main/java/sklep/util/PhotoUtil.java
+10
-8
application.properties
PC27-SklepSpring/src/main/resources/application.properties
+2
-0
No files found.
PC27-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
763ad912
...
...
@@ -21,6 +21,9 @@ public class ProductController {
@Autowired
private
ProductRepository
productRepository
;
@Autowired
private
PhotoUtil
photoUtil
;
@GetMapping
public
String
readAll
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
...
...
@@ -115,6 +118,6 @@ public class ProductController {
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
public
byte
[]
getPhoto
(
@PathVariable
(
"id"
)
Integer
productId
)
{
return
P
hotoUtil
.
readBytes
(
productId
);
return
p
hotoUtil
.
readBytes
(
productId
);
}
}
PC27-SklepSpring/src/main/java/sklep/util/PhotoUtil.java
View file @
763ad912
package
sklep
.
util
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatusCode
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ResponseStatusException
;
import
java.io.File
;
...
...
@@ -11,12 +13,14 @@ import java.nio.file.Path;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
@Component
public
class
PhotoUtil
{
private
static
final
String
EXT
=
".jpg"
;
public
static
File
getFile
(
int
productId
)
{
@Value
(
"${alx.photo_dir}"
)
private
String
dir
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
...
...
@@ -26,7 +30,7 @@ public class PhotoUtil {
}
}
public
static
byte
[]
readBytes
(
int
productId
)
{
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
...
...
@@ -35,7 +39,7 @@ public class PhotoUtil {
}
}
public
static
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
public
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
copy
(
inputStream
,
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
...
...
@@ -45,9 +49,7 @@ public class PhotoUtil {
}
}
private
static
Path
getPath
(
int
productId
)
{
//String dir = DBSettings.load().getProperty("photo_dir");
String
dir
=
"/home/patryk/sklep/foto"
;
private
Path
getPath
(
int
productId
)
{
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
...
...
PC27-SklepSpring/src/main/resources/application.properties
View file @
763ad912
...
...
@@ -4,3 +4,5 @@ spring.datasource.password=abc123
spring.mvc.view.prefix
=
/WEB-INF/templates/
spring.mvc.view.suffix
=
.jsp
alx.photo_dir
=
/home/patryk/sklep/foto
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment