Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
c166ce1d
Commit
c166ce1d
authored
Nov 27, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa zdjęć
parent
6dd615b1
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
1 deletions
+74
-1
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+11
-0
PhotoUtil.java
PC30-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
+58
-0
application.properties
PC30-SklepSpring/src/main/resources/application.properties
+3
-0
customer_form.jsp
...pring/src/main/webapp/WEB-INF/templates/customer_form.jsp
+2
-1
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
c166ce1d
...
...
@@ -14,8 +14,10 @@ import org.springframework.web.bind.annotation.GetMapping;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
sklep.model.Product
;
import
sklep.photo.PhotoUtil
;
import
sklep.repository.ProductRepository
;
import
sklep.utils.ExceptionUtils
;
...
...
@@ -25,6 +27,9 @@ public class ProductController {
@Autowired
private
ProductRepository
productRepository
;
@Autowired
private
PhotoUtil
photoUtil
;
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
...
...
@@ -120,4 +125,10 @@ public class ProductController {
}
}
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
public
byte
[]
getPhoto
(
@PathVariable
(
"id"
)
int
productId
)
{
return
photoUtil
.
readBytes
(
productId
);
}
}
PC30-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
c166ce1d
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
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ResponseStatusException
;
@Component
public
class
PhotoUtil
{
@Value
(
"${alx.photo_dir}"
)
private
String
photoDir
;
private
static
final
String
EXT
=
".jpg"
;
public
File
getFile
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
productId
);
}
}
public
byte
[]
readBytes
(
int
productId
)
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
// System.err.println(e);
throw
new
ResponseStatusException
(
HttpStatus
.
NOT_FOUND
,
"Cannot read photo for product id = "
+
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
(
photoDir
,
fileName
);
}
}
PC30-SklepSpring/src/main/resources/application.properties
View file @
c166ce1d
...
...
@@ -7,3 +7,5 @@ spring.mvc.view.suffix=.jsp
# server.port=9090
# server.servlet.context-path=/sklep
alx.photo_dir
=
/home/patryk/sklep/foto
\ No newline at end of file
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/customer_form.jsp
View file @
c166ce1d
...
...
@@ -7,7 +7,8 @@
<head>
<meta
charset=
"UTF-8"
>
<title>
Edycja danych klienta
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
/>
<c:url
var=
"style_url"
value=
"/styl.css"
/>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"${style_url}"
/>
</head>
<body>
<h1>
Edycja danych klienta
</h1>
...
...
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