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
93cdef4c
Commit
93cdef4c
authored
Apr 27, 2025
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SklepWeb - obsługa zdjęć
parent
b61118ef
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
170 additions
and
0 deletions
+170
-0
Photo.java
PC23-SklepWeb/src/main/java/sklep/photo/Photo.java
+43
-0
PhotoUpload.java
PC23-SklepWeb/src/main/java/sklep/photo/PhotoUpload.java
+39
-0
PhotoUtil.java
PC23-SklepWeb/src/main/java/sklep/photo/PhotoUtil.java
+54
-0
photo_upload.jsp
PC23-SklepWeb/src/main/webapp/photo_upload.jsp
+34
-0
No files found.
PC23-SklepWeb/src/main/java/sklep/photo/Photo.java
0 → 100644
View file @
93cdef4c
package
sklep
.
photo
;
import
java.io.IOException
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.ServletOutputStream
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
sklep.db.RecordNotFound
;
@WebServlet
(
"/photo"
)
public
class
Photo
extends
HttpServlet
{
/* Ten serwlet wczytuje z dysku plik ze zdjęciem o podanym numerze (z parametru productId).
* Aby odesłać odpowiedź "binarną" (a nie tekstową) używamy getOutputStream() zamiast getWriter().
* Aby przeglądarka wiedziała, że otrzymuje grafikę, ustawiamy content-type image/jpeg.
*/
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
String
parametrId
=
request
.
getParameter
(
"productId"
);
if
(
parametrId
==
null
)
{
return
;
}
try
{
int
id
=
Integer
.
parseInt
(
parametrId
);
byte
[]
bytes
=
PhotoUtil
.
readBytes
(
id
);
response
.
setContentType
(
"image/jpeg"
);
ServletOutputStream
output
=
response
.
getOutputStream
();
output
.
write
(
bytes
);
output
.
close
();
}
catch
(
RecordNotFound
e
)
{
response
.
setStatus
(
404
);
response
.
setContentType
(
"text/plain"
);
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
getWriter
().
println
(
"Nie ma zdjęcia dla produktu o nr "
+
parametrId
);
}
catch
(
Exception
e
)
{
response
.
setStatus
(
500
);
e
.
printStackTrace
();
}
}
}
PC23-SklepWeb/src/main/java/sklep/photo/PhotoUpload.java
0 → 100644
View file @
93cdef4c
package
sklep
.
photo
;
import
java.io.IOException
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.MultipartConfig
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
jakarta.servlet.http.Part
;
// Włączona obsługa zapytań multipart ("z załącznikami"). Maks rozmiar zapytania/pliku: 16M
@WebServlet
(
"/photo_upload"
)
@MultipartConfig
(
maxRequestSize
=
16
*
1024
*
1024
)
public
class
PhotoUpload
extends
HttpServlet
{
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
String
paramId
=
request
.
getParameter
(
"productId"
);
if
(
paramId
!=
null
)
{
int
productId
=
Integer
.
parseInt
(
paramId
);
Part
part
=
request
.
getPart
(
"plik"
);
if
(
part
!=
null
)
{
// przysłano plik
// Tutaj nazwa pliku jest dla nas bez znaczenia, ale gdybyśmy potrzebowali, to w ten sposób:
// String nazwaPliku = part.getSubmittedFileName();
// Przypisujemy bajty ze strumienia do pliku w katalogu ze zdjęciami:
PhotoUtil
.
writeStream
(
productId
,
part
.
getInputStream
());
}
}
}
catch
(
Exception
e
)
{
// wypisujemy błąd, ale metoda kończy się normalnie
e
.
printStackTrace
();
}
response
.
sendRedirect
(
"products9.jsp"
);
}
}
PC23-SklepWeb/src/main/java/sklep/photo/PhotoUtil.java
0 → 100644
View file @
93cdef4c
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
sklep.db.DBException
;
import
sklep.db.DBSettings
;
import
sklep.db.RecordNotFound
;
public
class
PhotoUtil
{
private
static
final
String
EXT
=
".jpg"
;
public
static
File
getFile
(
int
productId
)
throws
DBException
,
RecordNotFound
{
Path
path
=
getPath
(
productId
);
File
file
=
path
.
toFile
();
if
(
file
.
exists
())
{
return
file
;
}
else
{
throw
new
RecordNotFound
(
"Cannot read photo for product id = "
+
productId
);
}
}
public
static
byte
[]
readBytes
(
int
productId
)
throws
DBException
,
RecordNotFound
{
Path
path
=
getPath
(
productId
);
try
{
return
Files
.
readAllBytes
(
path
);
}
catch
(
IOException
e
)
{
// System.err.println(e);
throw
new
RecordNotFound
(
"Cannot read photo for product id = "
+
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
)
throws
DBException
{
String
dir
=
DBSettings
.
load
().
getProperty
(
"photo_dir"
);
String
fileName
=
productId
+
EXT
;
return
Paths
.
get
(
dir
,
fileName
);
}
}
PC23-SklepWeb/src/main/webapp/photo_upload.jsp
0 → 100644
View file @
93cdef4c
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"jakarta.tags.core"
%
>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Edycja zdjęcia
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Wgraj zdjęcie produktu
</h1>
<div>
Produkt nr
<strong>
${param.productId}
</strong></div>
<div>
Aktualne zdjęcie:
<br/>
<img
class=
"photo"
src=
"photo?productId=${param.productId}"
alt=
"Brak zdjęcia"
>
</div>
<
%
--
action
powoduje
,
ż
e
zapytanie
z
formularza
jest
wysy
ł
ane
pod
podany
adres
,
a
nie
bie
żą
cy
.
Aby
wys
ł
a
ć
zawarto
ść
pliku
(
a
nie
tylko
jego
nazw
ę),
nale
ż
y
ustawi
ć
enctype
jak
poni
ż
ej
.
Sam
plik
to
pole
formularza
typu
file
;
opr
ó
cz
niego
mog
ą
by
ć
inne
„
zwyk
ł
e
”
pola
.
Odpowiednio
trzeba
to
te
ż
obs
ł
u
ż
y
ć
w
serwlecie
-
patrz
klasa
PhotoUpload
.
--
%
>
<form
id=
"photo-form"
method=
"post"
action=
"photo_upload"
enctype=
"multipart/form-data"
>
<input
type=
"hidden"
name=
"productId"
value=
"${param.productId}"
>
<label
for=
"plik"
>
Wybierz plik ze zdjęciem
</label>
<input
id=
"plik"
type=
"file"
name=
"plik"
accept=
"image/jpeg"
>
<br>
<button>
Wyślij
</button>
</form>
<p>
[
<a
href=
"products9.jsp"
>
powrót do listy produktów
</a>
]
</p>
<p>
[
<a
href=
"index.html"
>
powrót do spisu treści
</a>
]
</p>
</body>
</html>
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