Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
a523da83
Commit
a523da83
authored
Apr 26, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa zdjęć
parent
cecea189
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
1 deletions
+72
-1
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+10
-1
PhotoUtil.java
PC30-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
+58
-0
application.properties
PC30-SklepSpring/src/main/resources/application.properties
+2
-0
product.jsp
...SklepSpring/src/main/webapp/WEB-INF/templates/product.jsp
+1
-0
products.jsp
...klepSpring/src/main/webapp/WEB-INF/templates/products.jsp
+1
-0
No files found.
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
a523da83
...
@@ -4,14 +4,15 @@ import java.util.List;
...
@@ -4,14 +4,15 @@ import java.util.List;
import
java.util.Optional
;
import
java.util.Optional
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.web.server.WebServerException
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.photo.PhotoUtil
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
@Controller
@Controller
...
@@ -20,6 +21,9 @@ public class ProductController {
...
@@ -20,6 +21,9 @@ public class ProductController {
@Autowired
@Autowired
private
ProductRepository
productRepository
;
private
ProductRepository
productRepository
;
@Autowired
private
PhotoUtil
photoUtil
;
@GetMapping
@GetMapping
public
String
wszystkieProdukty
(
Model
model
)
{
public
String
wszystkieProdukty
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
();
List
<
Product
>
products
=
productRepository
.
findAll
();
...
@@ -39,5 +43,10 @@ public class ProductController {
...
@@ -39,5 +43,10 @@ public class ProductController {
}
}
}
}
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
public
byte
[]
foto
(
@PathVariable
Integer
id
,
Model
model
)
{
return
photoUtil
.
readBytes
(
id
);
}
}
}
PC30-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
a523da83
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 @
a523da83
...
@@ -4,3 +4,5 @@ spring.datasource.password=abc123
...
@@ -4,3 +4,5 @@ spring.datasource.password=abc123
spring.mvc.view.prefix
=
/WEB-INF/templates/
spring.mvc.view.prefix
=
/WEB-INF/templates/
spring.mvc.view.suffix
=
.jsp
spring.mvc.view.suffix
=
.jsp
alx.photo_dir
=
/home/patryk/sklep/foto
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/product.jsp
View file @
a523da83
...
@@ -11,6 +11,7 @@
...
@@ -11,6 +11,7 @@
<h1>
Informacje o produkcie ${product.productId}
</h1>
<h1>
Informacje o produkcie ${product.productId}
</h1>
<div
class=
"product"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"/products/${product.productId}/photo"
alt=
""
/>
<p>
Towar
<a
href=
"/products/${product.productId}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Towar
<a
href=
"/products/${product.productId}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Stawka VAT:
<span
class=
"product-price"
>
${product.vat * 100}%
</span></p>
<p>
Stawka VAT:
<span
class=
"product-price"
>
${product.vat * 100}%
</span></p>
...
...
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/products.jsp
View file @
a523da83
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
<c:forEach
var=
"product"
items=
"${products}"
>
<c:forEach
var=
"product"
items=
"${products}"
>
<div
class=
"product"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"/products/${product.productId}/photo"
alt=
""
/>
<p>
Towar
<a
href=
"/products/${product.productId}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Towar
<a
href=
"/products/${product.productId}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
<p
class=
"product-description"
>
${product.description}
</p>
...
...
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