基于ssm的图片上传和将图片入数据库同时实现图片的预览和前段展示

在开发中图片上传用的比较多,所以这也是我们必会的一项的开发技能。直接上代码1、jsp这是一个整体的页面,实现信息的完善,重点的部分我已经加粗。图片实现预览实在js实现的,代码也加粗<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ tagli...
摘要由CSDN通过智能技术生成

在开发中图片上传用的比较多,所以这也是我们必会的一项的开发技能。直接上代码
1、jsp
这是一个整体的页面,实现信息的完善,重点的部分我已经加粗。
图片实现预览实在js实现的,代码也加粗

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html>

<head>
     <base href="<%=basePath %>" />
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>小白XBIT酒店</title>
    <meta name="description" content="这是一个 index 页面">
    <meta name="keywords" content="index">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="icon" type="image/png" href="static/i/favicon.png">
    <link rel="apple-touch-icon-precomposed" href="static/i/app-icon72x72@2x.png">
    <meta name="apple-mobile-web-app-title" content="Amaze UI" />
    <link rel="stylesheet" href="static/css/amazeui.min.css" />
    <link rel="stylesheet" href="static/css/admin.css">
    <link rel="stylesheet" href="static/css/app.css">
</head>

<body data-type="generalComponents">


  <header class="am-topbar am-topbar-inverse admin-header">
        <div class="am-topbar-brand">
            <a href="javascript:;" class="tpl-logo">
                <img src="static/img/logo.png" alt=&#
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是基于SSM和Vue实现图片的代码示例: 前端代码: ``` <template> <div> <input type="file" ref="fileInput" accept="image/*" @change="handleFileUpload"> <img v-if="imageUrl" :src="imageUrl" style="max-width: 100%;"> </div> </template> <script> export default { data() { return { imageUrl: '', file: null } }, methods: { handleFileUpload() { this.file = this.$refs.fileInput.files[0]; this.previewImage(this.file); this.uploadFile(this.file); }, previewImage(file) { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = e => { this.imageUrl = e.target.result; } }, async uploadFile(file) { const formData = new FormData(); formData.append('file', file); try { const response = await this.$axios.post('/upload', formData); console.log(response.data); } catch (error) { console.log(error); } } } } </script> ``` 后端代码: ``` @Controller public class UploadController { @PostMapping("/upload") @ResponseBody public String upload(@RequestParam("file") MultipartFile file) throws IOException { // 保存文件到服务器 File uploadFile = new File("/path/to/upload/" + file.getOriginalFilename()); file.transferTo(uploadFile); // 返回文件访问路径 return "http://your.domain.com/upload/" + file.getOriginalFilename(); } } ``` 其中,前端代码使用了Vue.js框架和axios库,后端使用了Spring MVC框架。在前端,我们使用`<input type="file">`元素来让用户选择上图片文件,然后使用FileReader API来图片;在上文件时,我们将文件封装为FormData对象,并使用axios库发送POST请求到后端的/upload接口;在后端,我们使用Spring MVC框架的@RequestParam注解来接收上的文件,并将文件保存到服务器上,最后返回文件的访问路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1029179954

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值