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
5a0ee948
Commit
5a0ee948
authored
Jul 29, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PhotoUtil jako static
parent
b02791d3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
4 deletions
+62
-4
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+7
-4
PhotoUtil.java
PC27-SklepSpring/src/main/java/sklep/util/PhotoUtil.java
+55
-0
No files found.
PC27-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
5a0ee948
...
@@ -5,12 +5,10 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -5,12 +5,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
sklep.util.PhotoUtil
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.List
;
...
@@ -114,4 +112,9 @@ public class ProductController {
...
@@ -114,4 +112,9 @@ public class ProductController {
return
"wyszukiwarka2"
;
return
"wyszukiwarka2"
;
}
}
@GetMapping
(
path
=
"/{id}/photo"
,
produces
=
"image/jpeg"
)
@ResponseBody
public
byte
[]
getPhoto
(
@PathVariable
(
"id"
)
Integer
productId
)
{
return
PhotoUtil
.
readBytes
(
productId
);
}
}
}
PC27-SklepSpring/src/main/java/sklep/util/PhotoUtil.java
0 → 100644
View file @
5a0ee948
package
sklep
.
util
;
import
org.springframework.http.HttpStatusCode
;
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
;
public
class
PhotoUtil
{
private
static
final
String
EXT
=
".jpg"
;
public
static
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
static
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
static
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
static
Path
getPath
(
int
productId
)
{
//String dir = DBSettings.load().getProperty("photo_dir");
String
dir
=
"/home/patryk/sklep/foto"
;
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
}
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