前端使用layui的图片上传,将文件base64编码,然后在后端使用转码类来操作base64编码,并保存图片到本地,继而获取文件地址,将文件地址保存到数据库中
1.使用layui的图片上传
infoset.jsp
layui. use ( 'upload' , function ( ) {
var $ = layui. jquery;
var upload = layui. upload;
var uploadInst = upload. render ( {
elem: '#test1' ,
url: 'http://localhost:8080/user?method=updateUserPhoto&id=' + id,
method: 'post' ,
auto: false ,
bindAction: '#ListAction' ,
choose: function ( obj) {
var files = obj. pushFile ( ) ;
obj. preview ( function ( index, file, result) {
console. log ( index) ;
console. log ( file) ;
console. log ( result) ;
$( '#demo1' ) . attr ( 'src' , result) ;
$. post ( "http://localhost:8080/user?method=updatePhoto" , { result: result, id: id} , function ( res) {
console. log ( "updatePhoto请求成功" ) ;
} , "text" ) ;
} )
} ,
done: function ( res) {
if ( res. code > 0 ) {
return layer. msg ( '上传失败' ) ;
}
} ,
error: function ( ) {
var demoText = $( '#demoText' ) ;
demoText. html ( '<span style="color: #c158ff;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>' ) ;
demoText. find ( '.demo-reload' ) . on ( 'click' , function ( ) {
uploadInst. upload ( ) ;
} ) ;
}
} ) ;
} ) ;
2.后端使用编码类转码,保存图片到本地
UserServlet
String basedata= req. getParameter ( "result" ) ;
Integer useridtwo = Integer. parseInt ( req. getParameter ( "id" ) ) ;
System. out. println ( "UserServlet中使用方法updatePhoto获取数据为:" + basedata) ;
String position= PhotoUtils. GenerateImage ( basedata, "reader" ) ;
Reader readertwo = userService. findUserById ( useridtwo) ;
if ( userService. updatePhoto ( readertwo, position) == 1 ) {
System. out. println ( "updatePhoto数据更新成功" ) ;
}
PhotoUtils
import org. apache. commons. fileupload. FileItem;
import sun. misc. BASE64Decoder;
import java. io. File;
import java. io. FileOutputStream;
import java. io. OutputStream;
import java. util. Date;
public class PhotoUtils {
public static String GenerateImage ( String imgStr, String wenjian)
{
System. out. print ( "已经收到了把字节码转化为图片的方法" ) ;
if ( imgStr == null)
return "error" ;
String str [ ] = imgStr. split ( "," ) ;
imgStr = str[ 1 ] ;
String imgInfo = str[ 0 ] ;
String imgExt = imgInfo. split ( "/" ) [ 1 ] . split ( ";" ) [ 0 ] ;
BASE64Decoder decoder = new BASE64Decoder ( ) ;
try
{
byte [ ] b = decoder. decodeBuffer ( imgStr) ;
for ( int i= 0 ; i< b. length; ++ i)
{
if ( b[ i] < 0 )
{
b[ i] += 256 ;
}
}
String imgFileReturn= "http://localhost:8080/image/" + getPhotoNewName ( imgExt, wenjian) ;
String imgFilePath = "E:\\JavaProject\\bookManage\\web\\image\\" + "\\" + getPhotoNewName ( imgExt, wenjian) ;
System. out. println ( imgFilePath) ;
OutputStream out = new FileOutputStream ( imgFilePath) ;
out. write ( b) ;
out. flush ( ) ;
out. close ( ) ;
return imgFileReturn;
}
catch ( Exception e)
{
return "" ;
}
}
public static int getSecondTimestamp ( Date date) {
if ( null == date) {
return 0 ;
}
String timestamp = String. valueOf ( date. getTime ( ) ) ;
System. out. println ( timestamp) ;
int length = timestamp. length ( ) ;
if ( length > 3 ) {
return Integer. valueOf ( timestamp. substring ( 0 , length- 3 ) ) ;
} else {
return 0 ;
}
}
public static String getPhotoNewName ( String imgExt, String wenjian) {
Date date= new Date ( ) ;
int second= getSecondTimestamp ( date) ;
String fileName= wenjian+ String. valueOf ( second) + "." + imgExt;
return fileName;
}
}
ReaderRepositoryImpl
@Override
public Reader findUserById ( int id) {
Connection connection = JdbcTools. getConnection ( ) ;
String sql = "select * from reader where id=?" ;
PreparedStatement statement = null;
ResultSet resultSet = null;
Reader reader = null;
try {
statement = connection. prepareStatement ( sql) ;
statement. setInt ( 1 , id) ;
resultSet = statement. executeQuery ( ) ;
if ( resultSet. next ( ) ) {
reader = new Reader ( resultSet. getInt ( 1 ) , resultSet. getString ( 2 ) , resultSet. getString ( 3 ) , resultSet. getString ( 4 ) , resultSet. getString ( 5 ) , resultSet. getString ( 6 ) , resultSet. getString ( 7 ) , resultSet. getInt ( 8 ) , resultSet. getString ( 10 ) ) ;
}
} catch ( SQLException e) {
e. printStackTrace ( ) ;
} finally {
JdbcTools. release ( connection, statement, resultSet) ;
}
return reader;
}