JavaWeb servlet jsp 使用七牛云API上传图片
Posted 霜序0.2℃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb servlet jsp 使用七牛云API上传图片相关的知识,希望对你有一定的参考价值。
文章目录
JavaWeb servlet jsp 使用七牛云API上传图片
这次是上一次的改进版本,相对上次好很多,上次很多作业混在一起没法随便分开
有兴趣的可以看看我上次写的:JavaWeb servlet jsp 上传图片,图片存入数据库,图片从数据库中取出,页面显示图片
页面其实用了一点jquery和layui来写
工具类
七牛云工具类
文档:
https://developer.qiniu.com/kodo/1239/java
package top.sehnsucht.util;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import lombok.Data;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/11/5
*/
public class Qiniu {
private static String ACCESS_KEY = "xxx";//填写你自己的
private static String SECRET_KEY = "xxx";
private static String BUCKET_NAME = "xxx";
private static String KEY = "";
private static String FILE_PATH = "";
/**
* 自定义返回的json格式
*/
public static void setJson() {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
StringMap putPolicy = new StringMap();
putPolicy.put("callbackUrl", "http://api.example.com/qiniu/upload/callback");
putPolicy.put("callbackBody", "{\\"key\\":\\"$(key)\\",\\"hash\\":\\"$(etag)\\",\\"bucket\\":\\"$(bucket)\\",\\"fsize\\":$(fsize)}");
putPolicy.put("callbackBodyType", "application/json");
long expireSeconds = 3600;
String upToken = auth.uploadToken(BUCKET_NAME, null, expireSeconds, putPolicy);
System.out.println(upToken);
}
/**
* 上传文件
*/
public static void uploadPic(String key, String filePath) {
KEY = key;FILE_PATH = filePath;
//构造一个带指定 Region 对象的配置类,这里可能需要根据自己情况改一下,点进源码看看情况,根据报错改region
Configuration cfg = new Configuration(Region.region2());
UploadManager uploadManager = new UploadManager(cfg);
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String upToken = auth.uploadToken(BUCKET_NAME);
try {
Response response = uploadManager.put(FILE_PATH, KEY, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
ex2.printStackTrace();
}
}
}
/**
* 返回地址
* @return
*/
public static String picUrl() {
String url = "http://r234valei.hn-bkt.clouddn.com";
String picUrl = url + "/" + KEY;
return picUrl;
}
}
JDBC工具类
package top.sehnsucht.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/10/29
*/
public class DBUtil {
// 定义数据库连接参数
public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/junior";
public static final String USERNAME = "root";
public static final String PASSWORD = "818181";
// 注册数据库驱动
static {
try {
Class.forName(DRIVER_CLASS_NAME);
} catch (ClassNotFoundException e) {
System.out.println("注册失败!");
e.printStackTrace();
}
}
// 获取连接
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
// 关闭连接
public static void closeConn(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭连接失败!");
e.printStackTrace();
}
}
}
//测试
public static void main(String[] args) throws SQLException {
System.out.println(DBUtil.getConn());
}
}
service
PhotoService
package top.sehnsucht.service;
import top.sehnsucht.vo.Photo;
import java.util.ArrayList;
import java.util.Map;
/**
* @Description: 图片业务接口
* @Author: Cai
* @CreateTime: 2021/11/5
*/
public interface PhotoService {
/**
* 获取所有图片路径
* @return
*/
ArrayList<Map<String, String>> getAllPhotos();
/**
* 添加图片到数据库
* @param name
* @param photourl
*/
void addPhoto(String name, String photourl);
/**
* 根据id查找图片
* @param id
* @return
*/
Photo selectPhotoById(Integer id);
}
PhotoServiceImpl
package top.sehnsucht.service.impl;
import top.sehnsucht.dao.PhotoDao;
import top.sehnsucht.service.PhotoService;
import top.sehnsucht.vo.Photo;
import java.util.ArrayList;
import java.util.Map;
/**
* @Description: 图片业务实现类
* @Author: Cai
* @CreateTime: 2021/11/5
*/
public class PhotoServiceImpl implements PhotoService {
private PhotoDao photoDao;
public PhotoServiceImpl(PhotoDao photoDao) {
this.photoDao = photoDao;
}
@Override
public ArrayList<Map<String, String>> getAllPhotos() {
return photoDao.getAllPhotos();
}
@Override
public void addPhoto(String name, String photourl) {
photoDao.addPhoto(name, photourl);
}
@Override
public Photo selectPhotoById(Integer id) {
return photoDao.getPhotoById(id);
}
}
Dao
PhotoDao
package top.sehnsucht.dao;
import top.sehnsucht.vo.Photo;
import java.util.ArrayList;
import java.util.Map;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/11/5
*/
public interface PhotoDao {
/**
* 返回ArrayList<Map<String,String>>,方便转成json传到前端
* @return
*/
ArrayList<Map<String,String>> getAllPhotos();
/**
* 添加photo到数据库中
* @param name
* @param photourl
*/
void addPhoto(String name, String photourl);
/**
* 获得最大的id
* @return
*/
Integer getMaxId();
/**
* 根据id查找图片
* @param id
* @return
*/
Photo getPhotoById(Integer id);
/**
* 获得数据库中图片的数量
* @return
*/
Integer photoCount();
}
PhotoDaoImpl
package top.sehnsucht.dao.impl;
import top.sehnsucht.dao.PhotoDao;
import top.sehnsucht.util.DBUtil;
import top.sehnsucht.vo.Photo;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* @Description:
* @Author: Cai
* @CreateTime: 2021/11/5
*/
public class PhotoDaoImpl implements PhotoDao {
@Override
public ArrayList<Map<String, String>> getAllPhotos() {
Connection conn = null;
PreparedStatement ps = null;
FileInputStream in = null;
ResultSet rs = null;
ArrayList<Map<String, String>> arrs = new ArrayList<>();
try {
conn = DBUtil.getConn();
String sql = "select id,name,photourl from photo order by id";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
while (rs.next()) {
Map<String, String> map = new HashMap();
Integer id = rs.getInt(1);
String name = rs.getString(2);
String photourl = rs.getString(3);
System.out.println("查询: " + id + " " + name + " " + photourl);
map.put("id", id.toString());
map.put("name", name);
// map.put("photourl",photourl);
arrs.add(map);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn);
if (null != ps) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return arrs;
}
@Override
public Photo getPhotoById(Integer id) {
Connection conn = null;
PreparedStatement ps = null;
FileInputStream in = null;
ResultSet rs = null;
Integer thisId = null;
String name = null;
String photourl = null;
try {
conn = DBUtil.getConn();
System.out.println("id:" + id);
String sql = "select * from photo where id = " + id;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
while (rs.next()) {
thisId = rs.getInt(1);
name = rs.getString(2);
photourl = rs.getString(3);
System.out.println(thisId);
System.out.println(name);
System.out.println(photourl);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn);
if (null != ps) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return new Photo(thisId, name, photourl);
}
@Override
public Integer photoCount() {
Connection conn = null;
PreparedStatement ps = null;
FileInputStream in = null;
ResultSet rs = null;
int ans = 0;
try {
conn = DBUtil.getConn();
String sql = "select id,name from photo ";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
while (rs.next()) {
ans++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn);
if (null != ps) {
try {
ps.close();
} catch 以上是关于JavaWeb servlet jsp 使用七牛云API上传图片的主要内容,如果未能解决你的问题,请参考以下文章