Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
alx_java2b_20250412
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
alx_java2b_20250412
Commits
fc9c9372
Commit
fc9c9372
authored
May 31, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PhotoUtil i wyświetlanie zdjęć
parent
ca963626
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
0 deletions
+86
-0
PhotoController.java
...klepSpring/src/main/java/sklep/photo/PhotoController.java
+19
-0
PhotoUtil.java
PC37-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
+65
-0
application.properties
PC37-SklepSpring/src/main/resources/application.properties
+2
-0
No files found.
PC37-SklepSpring/src/main/java/sklep/photo/PhotoController.java
0 → 100644
View file @
fc9c9372
package
sklep
.
photo
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@Controller
public
class
PhotoController
{
@Autowired
private
PhotoUtil
photoUtil
;
@GetMapping
(
path
=
"/products/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
public
byte
[]
getPhoto
(
@PathVariable
int
id
)
{
return
photoUtil
.
readBytes
(
id
);
}
}
PC37-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
fc9c9372
package
sklep
.
photo
;
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
;
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
;
@Component
public
class
PhotoUtil
{
private
static
final
String
EXT
=
".jpg"
;
@Value
(
"${alx.photo_dir}"
)
private
String
dir
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
ResponseStatusException
(
HttpStatusCode
.
valueOf
(
404
),
"Brak zdjęcia nr "
+
productId
);
}
}
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
throw
new
ResponseStatusException
(
HttpStatusCode
.
valueOf
(
404
),
"Brak zdjęcia nr "
+
productId
);
}
}
public
void
writeStream
(
int
productId
,
InputStream
inputStream
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
copy
(
inputStream
,
path
,
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
Exception
e
)
{
// wypisujemy błąd, ale metoda kończy się normalnie
e
.
printStackTrace
();
}
}
private
Path
getPath
(
int
productId
)
{
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
public
void
writeBytes
(
int
productId
,
byte
[]
bytes
)
{
try
{
Path
path
=
getPath
(
productId
);
Files
.
write
(
path
,
bytes
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
PC37-SklepSpring/src/main/resources/application.properties
View file @
fc9c9372
...
@@ -5,3 +5,5 @@ spring.mvc.view.suffix=.jsp
...
@@ -5,3 +5,5 @@ spring.mvc.view.suffix=.jsp
spring.datasource.url
=
jdbc:postgresql://localhost/sklep
spring.datasource.url
=
jdbc:postgresql://localhost/sklep
spring.datasource.username
=
alx
spring.datasource.username
=
alx
spring.datasource.password
=
abc123
spring.datasource.password
=
abc123
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