Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
1afb874a
Commit
1afb874a
authored
Jun 27, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
obsługa zdjęć w SklepSpring
parent
3f66a77c
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
76 additions
and
8 deletions
+76
-8
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+11
-4
PhotoUtil.java
PC33-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
+57
-0
application.properties
PC33-SklepSpring/src/main/resources/application.properties
+1
-0
index.jsp
PC33-SklepSpring/src/main/webapp/WEB-INF/templates/index.jsp
+1
-0
product.jsp
...SklepSpring/src/main/webapp/WEB-INF/templates/product.jsp
+5
-4
products.jsp
...klepSpring/src/main/webapp/WEB-INF/templates/products.jsp
+1
-0
No files found.
PC33-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
1afb874a
...
...
@@ -4,11 +4,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.data.domain.Sort
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.*
;
import
sklep.model.Product
;
import
sklep.photo.PhotoUtil
;
import
sklep.repository.ProductRepository
;
import
java.math.BigDecimal
;
...
...
@@ -21,6 +19,9 @@ public class ProductController {
@Autowired
private
ProductRepository
productRepository
;
@Autowired
private
PhotoUtil
photoUtil
;
@GetMapping
public
String
odczytajProdukty
(
Model
model
)
{
List
<
Product
>
products
=
productRepository
.
findAll
(
Sort
.
by
(
"id"
));
...
...
@@ -68,4 +69,10 @@ public class ProductController {
model
.
addAttribute
(
"products"
,
products
);
return
"wyszukiwarka2"
;
}
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
public
byte
[]
getPhoto
(
@PathVariable
int
id
)
{
return
photoUtil
.
readBytes
(
id
);
}
}
PC33-SklepSpring/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
1afb874a
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
);
}
}
PC33-SklepSpring/src/main/resources/application.properties
View file @
1afb874a
...
...
@@ -6,3 +6,4 @@ spring.datasource.url=jdbc:postgresql://localhost/sklep
spring.datasource.username
=
alx
spring.datasource.password
=
abc123
alx.photo_dir
=
/home/patryk/sklep/foto
PC33-SklepSpring/src/main/webapp/WEB-INF/templates/index.jsp
View file @
1afb874a
...
...
@@ -32,6 +32,7 @@
<li><a
href=
"/products/szukaj?name=pralka"
>
wyszukiwarka/pralka
</a></li>
<li><a
href=
"/products/new"
>
nowy produkt
</a></li>
<li><a
href=
"/products/1/edit"
>
edycja produktu
</a></li>
<li><a
href=
"/products/2/photo"
>
przykładowe zdjęcie
</a></li>
</ul>
<h2>
Edycja klienta
</h2>
...
...
PC33-SklepSpring/src/main/webapp/WEB-INF/templates/product.jsp
View file @
1afb874a
...
...
@@ -11,10 +11,11 @@
<h1>
Informacje o produkcie ${product.id}
</h1>
<div
class=
"product"
>
<p>
Towar
<a
href=
"/products/${product.id}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Stawka VAT:
<span
class=
"product-price"
>
${product.vat * 100}%
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
<img
class=
"photo"
src=
"/products/${product.id}/photo"
alt=
""
/>
<p>
Towar
<a
href=
"/products/${product.id}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Stawka VAT:
<span
class=
"product-price"
>
${product.vat * 100}%
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
</div>
<p><a
href=
"/products"
>
Przejdź do listy produktów
</a></p>
...
...
PC33-SklepSpring/src/main/webapp/WEB-INF/templates/products.jsp
View file @
1afb874a
...
...
@@ -13,6 +13,7 @@
<c:forEach
var=
"product"
items=
"${products}"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"/products/${product.id}/photo"
alt=
""
/>
<p>
Towar
<a
href=
"/products/${product.id}"
class=
"product-name"
>
${product.productName}
</a></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></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