Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
aserver
Project
Project
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
15所TongWeb
aserver
Commits
99827d54
Commit
99827d54
authored
Aug 27, 2020
by
李德才
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加上传头像处理
parent
1ea011a2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
0 deletions
+59
-0
UserController.java
...n/java/com/archser/aserver/controller/UserController.java
+59
-0
No files found.
src/main/java/com/archser/aserver/controller/UserController.java
View file @
99827d54
package
com
.
archser
.
aserver
.
controller
;
package
com
.
archser
.
aserver
.
controller
;
import
com.jfinal.upload.UploadFile
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -18,6 +21,7 @@ import com.jfinal.kit.Ret;
...
@@ -18,6 +21,7 @@ import com.jfinal.kit.Ret;
import
com.jfinal.plugin.activerecord.Db
;
import
com.jfinal.plugin.activerecord.Db
;
import
com.jfinal.plugin.activerecord.Record
;
import
com.jfinal.plugin.activerecord.Record
;
import
com.jfinal.plugin.redis.Redis
;
import
com.jfinal.plugin.redis.Redis
;
import
sun.misc.BASE64Encoder
;
/**
/**
* 用户相关操作
* 用户相关操作
...
@@ -150,4 +154,59 @@ public class UserController extends Controller {
...
@@ -150,4 +154,59 @@ public class UserController extends Controller {
}
}
}
}
public
void
saveImage
()
{
String
username
=
this
.
getAttr
(
"username"
);
User
user
=
User
.
dao
.
template
(
"getUser"
,
username
).
findFirst
();
UploadFile
file
=
getFile
();
File
imgFile
=
file
.
getFile
();
if
(
imgFile
!=
null
)
{
try
{
byte
[]
imgBase
=
imageBase64
(
imgFile
);
if
(
imgBase
!=
null
)
{
user
.
setImage
(
imgBase
);
user
.
update
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
imgFile
.
delete
();
renderJson
(
Ret
.
ok
());
}
else
{
renderJson
(
Ret
.
fail
(
"msg"
,
"修改头像失败"
));
}
}
public
byte
[]
imageBase64
(
File
file
)
{
FileInputStream
fis
;
byte
[]
bytes
=
null
;
try
{
fis
=
new
FileInputStream
(
file
);
bytes
=
new
byte
[
fis
.
available
()];
fis
.
read
(
bytes
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
bytes
;
}
public
void
getImage
()
{
String
username
=
this
.
getAttr
(
"username"
);
User
user
=
User
.
dao
.
template
(
"getUser"
,
username
).
findFirst
();
byte
[]
image
=
user
.
getImage
();
if
(
image
==
null
)
{
renderJson
(
Ret
.
ok
(
"baseStr"
,
image
));
return
;
}
String
baseStr
=
image2Base64
(
image
);
renderJson
(
Ret
.
ok
(
"baseStr"
,
baseStr
));
}
public
String
image2Base64
(
byte
[]
image
)
{
BASE64Encoder
encoder
=
new
BASE64Encoder
();
String
base64Str
=
encoder
.
encode
(
image
);
return
base64Str
;
}
}
}
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