提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
本项目为:Java课程设计基于SwingUI、Mybatis、Vue、Springboot完成基本的增删改查
能力有限,记录自己的学习过程
提示:以下是本篇文章正文内容,下面案例可供参考
一、电商管理系统架构?
数据库设计
部分展示
二、核心代码
1.商品图片上传
客户端
代码如下:
public class Upload {
public static String uploadIMG(String fileName) throws IOException {
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定义数据分隔线
String BOUNDARY = "========7d4a6d158c932";
URL url = new URL(Config.BASE_URL+"/upload");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求头参数
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
//conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" );
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 上传文件
File file = new File(fileName);
StringBuilder sb = new StringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
// 文件参数,photo参数名可以随意修改
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + fileName
+ "\"" + newLine);
sb.append("Content-Type:image/jpeg");
// 参数头设置完以后需要两个换行,然后才是参数内容
sb.append(newLine);
sb.append(newLine);
// 将参数头的数据写入到输出流中
out.write(sb.toString().getBytes());
// 数据输入流,用于读取文件数据
DataInputStream in = new DataInputStream(new FileInputStream(file));
byte[] bufferOut = new byte[1024];
int bytes = 0;
// 每次读1KB数据,并且将文件数据写入到输出流中
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
// 最后添加换行
out.write(newLine.getBytes());
in.close();
// 定义最后数据分隔线,即--加上BOUNDARY再加上--。
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY + boundaryPrefix + newLine)
.getBytes();
// 写上结尾标识
out.write(end_data);
out.flush();
out.close();
// 定义BufferedReader输入流来读取URL的响应 ----读取返回的结果
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line = null;
StringBuffer stb=new StringBuffer();
while ((line = reader.readLine()) != null) {
stb.append(line);
}
String s = stb.toString();
UploadIMGJson res = JSON.parseObject(s, UploadIMGJson.class);
String r=res.getUploadedFilePath();
String re = r.replace("\\", "/");
return re;
}
服务端
@RestController
@RequestMapping
public class uploadfile {
@PostMapping("/upload")
public Object uploadHandler(HttpServletRequest request, String title, MultipartFile file) {
Map<String, Object> resultMap = new LinkedHashMap<>();
resultMap.put("title", title);
resultMap.put("fileName", file.getName()); // 文件名
resultMap.put("originalFilename", file.getOriginalFilename()); // 原始名称
resultMap.put("content-type", file.getContentType()); // 文件类型
resultMap.put("fileSize", file.getSize() / 1024 + "K"); // 文件大小
try {
// 保存文件
String uploadedFilePath = saveFile(request, file.getInputStream(), file.getOriginalFilename()
.substring(file.getOriginalFilename().lastIndexOf(".") + 1));
resultMap.put("uploadedFilePath", uploadedFilePath); // 文件
return resultMap;
} catch (IOException e) {
System.err.println("error-path: /upload/file, message: " + e.getMessage());
return e.getMessage();
}
}
public String saveFile(HttpServletRequest request, InputStream input, String ext) throws IOException {
String realPath= System.getProperty("user.dir") + "\\src\\main\\resources\\static\\image\\";
//String realPath= System.getProperty("user.dir") + "\\src\\main\\resources\\iii\\";
//System.out.println(realPath);
File file = new File(realPath);
if (!file.getParentFile().exists()) { // 目录不存在
file.mkdirs(); // 创建多级目录
}
String filePath = realPath + UUID.randomUUID() + "." + ext;
// 取的文件输出流
OutputStream out = new FileOutputStream(filePath);
byte[] data = new byte[2048]; // 缓冲数组2KB
int len = 0; // 读取字节长度
while ((len = input.read(data)) != -1) {
out.write(data, 0, len); // 文件写入磁盘
}
if (input != null) {
input.close();
}
out.close();
System.out.println(filePath);
return filePath;
}
}
2.读入数据
数据分页
代码如下:
Bean层:
@Data
public class Goods {
private String id;
private String name;
private float price;
private String img;
private Date firsttime;
private Date updatetime;
private String content;
private String sellerid;
}
Dao层
@Select("select * from shop_detail LIMIT #{pageindex},#{total}")
public List<Goods> pageShops(@Param("pageindex") Integer pageindex, @Param("total") Integer total);
Service层
public static List<Goods> pageShops(Integer pageindex,Integer total){
List<Goods> pgoods =goods_allDao.pageShops(pageindex*total,total);
System.out.println(pgoods);
return pgoods;
}
bu1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (page.getCurrentPage() == 1) {
JOptionPane.showMessageDialog(null, "当前页为第一页,不可以再往前", "发生错误", JOptionPane.ERROR_MESSAGE);
page.setCurrentPage(page.getCurrentPage());
list = GoodsService.pageShops(page.getCurrentPage() - 1,total);
try {
show(0, list);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
} else {
page.setCurrentPage(page.getCurrentPage() - 1);
list = GoodsService.pageShops(page.getCurrentPage() - 1,total);
}
lb.setText("当前页: " + (page.getCurrentPage()) + "");
list = GoodsService.pageShops(page.getCurrentPage() - 1,total);
try {
show(0, list);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
});
// 下一页
bu2.addMouseListener(new MouseAdapter() {
@SneakyThrows
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (page.getCurrentPage() == page.getEndPage()) {
JOptionPane.showMessageDialog(null, "当前页为最后一页,不可以再往后", "发生错误", JOptionPane.ERROR_MESSAGE);
page.setCurrentPage(page.getCurrentPage());
//list = GoodsService.pageShops(page.getCurrentPage(),total);
try {
show(0, list);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
} else {
//System.out.println("331当前页码:"+page.getCurrentPage());
list = GoodsService.pageShops(page.getCurrentPage(),total);
page.setCurrentPage(page.getCurrentPage() + 1);
}
lb.setText("当前页: " + (page.getCurrentPage()) + "");
//list=GoodsService.pageShops(page.getCurrentPage());
try {
show(0, list);
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
});
获取并显示数据函数
public void show(int start, List<Goods> list) throws MalformedURLException {
pl2.removeAll();
int total_in_page = row * col;
JPanel jp[] = new JPanel[total_in_page];
// System.out.println(list.size());
JPanel empty=new JPanel();
JLabel empl=new JLabel();
empty.add(empl);
JPanel emp=new Panel_main().panel();
for (int i = 0; i < jp.length; i++) {
if((list.size()<=jp.length)){
if(i>=list.size()){
pl2.add(emp);
}else{
jp[i] = new Panel_main().panel(list.get(start + i));
pl2.add(jp[i]);
}
}else{
jp[i] = new Panel_main().panel(list.get(start + i));
pl2.add(jp[i]);
}
pl2.validate();
// 重绘组件
pl2.repaint();
}
jf.setVisible(true);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}