当前位置: 首页 > news >正文

企业网站制作优化开网店要建网站平台吗

企业网站制作优化,开网店要建网站平台吗,wordpress开发视频教程,建立门户公司网站一、文件上传——简介 文件上传的简介#xff1a;文件上传是指将本地计算机中的文件传输到网络上的服务器或另一台计算机上的过程。在 Web 开发中#xff0c;文件上传通常指的是将用户通过 Web 页面提交的文件#xff08;如图像、文档、音频、视频等#xff09;传输到服务器…一、文件上传——简介 文件上传的简介文件上传是指将本地计算机中的文件传输到网络上的服务器或另一台计算机上的过程。在 Web 开发中文件上传通常指的是将用户通过 Web 页面提交的文件如图像、文档、音频、视频等传输到服务器端的操作。 简单来说文件上传是指将本地图片、视频、音频等文件上传到服务器上供其他用户浏览或下载的过程。 文件上传通常涉及以下几个主要组件 客户端指的是文件上传的发起方通常是用户在 Web 浏览器中通过表单提交文件。 服务器端指的是接收并处理文件上传请求的计算机系统或服务器。 上传表单通过 HTML 表单元素来实现文件上传功能。表单需要设置 enctype 属性为 “multipart/form-data”并包含一个文件输入框用于选择要上传的文件。 文件处理逻辑服务器端接收到文件上传请求后需要将上传的文件保存到指定的位置并可能进行进一步的处理如文件存储、文件重命名、文件格式验证等。 文件上传控制器在 Web 开发框架中通常需要编写文件上传的处理逻辑这些逻辑通常由服务器端的控制器或处理器来处理。 文件上传在 Web 开发中非常常见常见的应用场景包括但不限于用户头像上传、文件分享、数据备份等。 二、文件上传——入门 文件上传的步骤 先创建一个springboot工程这个常识了不展示了 编写一个上传文件的前端html文件 编写一个controller 项目结构 文件上传前端的三要素 请求方式是post需要设置 enctype“multipart/form-data” 属性以支持文件上传form 元素应该包含一个 input typefile 元素用于让用户选择要上传的文件。 编写的html文件 !DOCTYPE html html langen headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0title文件上传/title /head bodyh2上传文件/h2form action/upload methodpost enctypemultipart/form-datainput typefile namefile idfileToUploadinput typesubmit value上传 namesubmit /form/body /html 编写的controller package com.knife.controller;import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;RestController public class UploadController {PostMapping(/upload)public void upload(MultipartFile file){System.out.println(file file);}} 运行截图通过xml配置的路径去访问html文件然后点击上传文件 file内容不等于null说明文件上传成功。 总结服务端要想接收到从前端页面上传的文件需要使用到一个apiMultipartFile通过该api来接收上传的文件。而上传上来的文件是一个临时文件当文件上传这次请求完成之后这些临时文件会自动删除。因此在上传文件的同时应该把文件保存起来。 三、文件上传——本地存储—第一版 在服务端接收到上传上来的文件之后将文件存储在本地服务器磁盘中。 基础实现在上面的文件上传——入门的基础上修改一下controller就行。 package com.knife.controller;import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.IOException;RestController public class UploadController {PostMapping(/upload)public void upload(MultipartFile file) throws IOException {System.out.println(file file);// 获取源文件的文件名String originalFilename file.getOriginalFilename();// 把上传上来的文件存储到磁盘上:transferTo的参数是一个文件file.transferTo(new File(E:\\images\\ originalFilename));}} 只需把上面的controller改成上面一样然后运行然后如果你的路径不对或者你盘符的目录不存在就会报错了因为这里没有对异常处理只是简单地把异常抛出去了 最快的解决办法就是修改本地存储的路径输入一个存在的目录的路径。 最佳解决办法如下更改controller package com.knife.controller;import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.IOException; import java.util.UUID;RestController public class UploadController {PostMapping(/upload)public void upload(MultipartFile file) throws IOException {System.out.println(file file);// 1. 获取源文件的文件名String originalFilename file.getOriginalFilename();// 定义要上传的路径String fileUploadPath E:\\images\\ originalFilename;// 创建要上传的文件File uploadFile new File(fileUploadPath);// 判断配置的文件目录是否存在若不存在则创建一个新的文件目录File parentFile uploadFile.getParentFile();if(!parentFile.exists()) {parentFile.mkdirs();}System.out.println(uploadFile);file.transferTo(uploadFile);}} 运行 四、文件上传——本地存储—第二版 本地存储第一版存在的问题 使用原始文件名进行存储时如果同时上传了文件名一样的文件那么后面上传的文件会把前面上传的文件给覆盖。 解决保证每一个文件的文件名是唯一的文件名不能重复 使用uuid工具通用唯一标识码——使用生成的uuid码原始文件名的后缀 不管文件路径的报错的写法和上面文件上传——本地存储—第一版就只有controller的差别 package com.knife.controller;import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.IOException; import java.util.UUID;RestController public class UploadController {PostMapping(/upload)public void upload(MultipartFile file) throws IOException {System.out.println(file file);// 1. 获取源文件的文件名String originalFilename file.getOriginalFilename();// 2. 构造唯一的文件名 -- 使用uuid通用唯一识别码长度固定的字符串而且是唯一的 原始文件文件后缀名 // 2.1. 通过字符串的截取获取文件的后缀名int index originalFilename.lastIndexOf(.);String extname originalFilename.substring(index); // 2.2 把uuid和文件后缀名合并String newFileName UUID.randomUUID().toString() extname;System.out.println(newFileName newFileName); // 把上传上来的文件存储到磁盘上指定一个路径file.transferTo(new File(E:\\ newFileName));}} 解决文件路径的报错的写法和上面文件上传——本地存储—第一版就只有controller的差别 package com.knife.controller;import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.IOException; import java.util.UUID;RestController public class UploadController {PostMapping(/upload)public void upload(MultipartFile file) throws IOException {System.out.println(file file);// 1. 获取源文件的文件名String originalFilename file.getOriginalFilename();// 2. 构造唯一的文件名 -- 使用uuid通用唯一识别码长度固定的字符串而且是唯一的 原始文件文件后缀名 // 2.1. 通过字符串的截取获取文件的后缀名int index originalFilename.lastIndexOf(.);String extname originalFilename.substring(index); // 2.2 把uuid和文件后缀名合并String newFileName UUID.randomUUID().toString() extname;// 定义要上传的路径String fileUploadPath E:\\images\\ newFileName;// 创建要上传的文件File uploadFile new File(fileUploadPath);// 判断配置的文件目录是否存在若不存在则创建一个新的文件目录File parentFile uploadFile.getParentFile();if(!parentFile.exists()) {parentFile.mkdirs();}System.out.println(uploadFile); // 把上传上来的文件存储到磁盘上指定一个路径file.transferTo(uploadFile);}} 运行 文件上传大小限制的问题在配置文件里面进行配置 如果是application.properties则如上图一样配置就行如果是application.yml则如下配置即可 application.yml server:port: 8088 # 配置访问服务器的端口spring:servlet:multipart:max-file-size: 10MB # 配置单个文件最大上传大小max-request-size: 100MB # 配置单个请求最大上传文件的大小一次可以上传多个文件即多个文件的总和也算 本地存储存在的问题 五、文件上传——对象存储——阿里云OOS 五、1、参照官方SDK编写入门程序 第一步在Maven工程中使用OSS Java SDK只需在pom.xml中加入相应依赖即可。在dependencies中加入如下内容参考官方文档的说明 dependencygroupIdcom.aliyun.oss/groupIdartifactIdaliyun-sdk-oss/artifactIdversion3.15.1/version /dependency如果使用的是Java 9及以上的版本则需要添加JAXB相关依赖。添加JAXB相关依赖示例代码如下 dependencygroupIdjavax.xml.bind/groupIdartifactIdjaxb-api/artifactIdversion2.3.1/version /dependency dependencygroupIdjavax.activation/groupIdartifactIdactivation/artifactIdversion1.1.1/version /dependency !-- no more than 2.3.3-- dependencygroupIdorg.glassfish.jaxb/groupIdartifactIdjaxb-runtime/artifactIdversion2.3.3/version /dependency第二步复制上传文件入门代码官方文档官方文档入门程序例子然后再根据自己的实际情况改写 import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectResult; import java.io.FileInputStream; import java.io.InputStream;public class Demo {public static void main(String[] args) throws Exception {// Endpoint以华东1杭州为例其它Region请按实际情况填写。String endpoint https://oss-cn-hangzhou.aliyuncs.com;// 从环境变量中获取访问凭证。运行本代码示例之前请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。EnvironmentVariableCredentialsProvider credentialsProvider CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();// 填写Bucket名称例如examplebucket。String bucketName examplebucket;// 填写Object完整路径完整路径中不能包含Bucket名称例如exampledir/exampleobject.txt。String objectName exampledir/exampleobject.txt;// 填写本地文件的完整路径例如D:\\localpath\\examplefile.txt。// 如果未指定本地路径则默认从示例程序所属项目对应本地路径中上传文件流。String filePath D:\\localpath\\examplefile.txt;// 创建OSSClient实例。OSS ossClient new OSSClientBuilder().build(endpoint, credentialsProvider);try {InputStream inputStream new FileInputStream(filePath);// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest new PutObjectRequest(bucketName, objectName, inputStream);// 创建PutObject请求。PutObjectResult result ossClient.putObject(putObjectRequest);} catch (OSSException oe) {System.out.println(Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.);System.out.println(Error Message: oe.getErrorMessage());System.out.println(Error Code: oe.getErrorCode());System.out.println(Request ID: oe.getRequestId());System.out.println(Host ID: oe.getHostId());} catch (ClientException ce) {System.out.println(Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network.);System.out.println(Error Message: ce.getMessage());} finally {if (ossClient ! null) {ossClient.shutdown();}}} } 改写后的入门程序主要修改自己的endpoint、accessKeyId、bucketName、objectName、filePath package com.knife;import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.PutObjectRequest; import com.aliyun.oss.model.PutObjectResult; import java.io.FileInputStream; import java.io.InputStream;public class uploadOss{public static void main(String[] args) throws Exception {// Endpoint华北2北京请按实际情况填写。String endpoint oss-cn-beijing.aliyuncs.com; // 根据自己的实际情况填写// 从环境变量中获取访问凭证。运行本代码示例之前请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 // EnvironmentVariableCredentialsProvider credentialsProvider CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 使用accessKeyId、accessKeyIdSecret替代。String accessKeyId 你自己的密钥id; // 根据自己的实际情况填写String accessKeyIdSecret 你自己的密钥;// 根据自己的实际情况填写// 填写Bucket名称例如examplebucket。String bucketName 你自己的bucketName;// 根据自己的实际情况填写// 填写Object完整路径完整路径中不能包含Bucket名称例如exampledir/exampleobject.txt。String objectName 1.jpg; //设置我们上传的文件最终在阿里云上面是什么名字// 填写本地文件的完整路径例如D:\\localpath\\examplefile.txt。// 如果未指定本地路径则默认从示例程序所属项目对应本地路径中上传文件流。String filePath C:\\Users\\qq351\\Pictures\\Saved Pictures\\1.jpg;// 创建OSSClient实例。 // OSS ossClient new OSSClientBuilder().build(endpoint, credentialsProvider);OSS ossClient new OSSClientBuilder().build(endpoint, accessKeyId,accessKeyIdSecret);try {InputStream inputStream new FileInputStream(filePath);// 创建PutObjectRequest对象。PutObjectRequest putObjectRequest new PutObjectRequest(bucketName, objectName, inputStream);// 创建PutObject请求。PutObjectResult result ossClient.putObject(putObjectRequest);} catch (OSSException oe) {System.out.println(Caught an OSSException, which means your request made it to OSS, but was rejected with an error response for some reason.);System.out.println(Error Message: oe.getErrorMessage());System.out.println(Error Code: oe.getErrorCode());System.out.println(Request ID: oe.getRequestId());System.out.println(Host ID: oe.getHostId());} catch (ClientException ce) {System.out.println(Caught an ClientException, which means the client encountered a serious internal problem while trying to communicate with OSS, such as not being able to access the network.);System.out.println(Error Message: ce.getMessage());} finally {if (ossClient ! null) {ossClient.shutdown();}}} } 路径获取 运行就可以传进去了 通过浏览器访问该图片的路径时会直接下载该图片但是在页面显示出来挺简单的就是使用一个imag标签然后把路径放在src下即可 image srchttps://cangqiongwaimaiknife.oss-cn-beijing.aliyuncs.com/1.jpg六、在项目中使用OOS文件上传 在项目中一般会把文件上传的方法封装成一个工具类然后再使用。 在配置文件里面对oss的参数进行配置 #阿里云OSS aliyun:oss:endpoint: https://oss-cn-hangzhou.aliyuncs.comaccessKeyId: LTAI4GCH1vX6DKqJWxd6nEuWaccessKeySecret: yBshYweHOpqDuhCArrVHwIiBKpyqSLbucketName: web-tlias属性配置类读取配置文件里面的属性 import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;Data Component ConfigurationProperties(prefix aliyun.oss) public class AliOSSProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName; } 封装工具类 import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.UUID;/*** 阿里云 OSS 工具类*/ Component public class AliOSSUtils {Autowiredprivate AliOSSProperties aliOSSProperties;/*** 实现上传图片到OSS*/public String upload(MultipartFile file) throws IOException {//获取阿里云OSS参数String endpoint aliOSSProperties.getEndpoint();String accessKeyId aliOSSProperties.getAccessKeyId();String accessKeySecret aliOSSProperties.getAccessKeySecret();String bucketName aliOSSProperties.getBucketName();// 获取上传的文件的输入流InputStream inputStream file.getInputStream();// 避免文件覆盖String originalFilename file.getOriginalFilename();String fileName UUID.randomUUID().toString() originalFilename.substring(originalFilename.lastIndexOf(.));//上传文件到 OSSOSS ossClient new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);ossClient.putObject(bucketName, fileName, inputStream);//文件访问路径String url endpoint.split(//)[0] // bucketName . endpoint.split(//)[1] / fileName;// 关闭ossClientossClient.shutdown();return url;// 把上传到oss的路径返回}} controller类上使用 import com.itheima.pojo.Result; import com.itheima.utils.AliOSSUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import java.io.File; import java.io.IOException; import java.util.UUID;RestController public class UploadController {Autowiredprivate AliOSSUtils aliOSSUtils;PostMapping(/upload)public Result upload(MultipartFile image) throws IOException {log.info(文件上传, 文件名: {}, image.getOriginalFilename());//调用阿里云OSS工具类进行文件上传String url aliOSSUtils.upload(image);log.info(文件上传完成,文件访问的url: {}, url);return Result.success(url);}}
http://www.dnsts.com.cn/news/222948.html

相关文章:

  • 自助建站工具好单库如何做网站
  • 网站飘动广告代码四川省住房和城乡建设厅门户网站
  • 企业微网站与手机微信号北京网站建设公司资讯
  • 网站建设家居cms影视建站系统
  • 有没有专门做老年婚介的网站成都网站建设网
  • 网站建设一般是用哪个软件加强门户网站建设的方案
  • 建设企业网站的需求分析seo是做网站
  • 网站中的自助报价系统长春网站推广方式
  • 温州市建设小学大南网站苏州注册公司需要多少钱
  • 岳阳网站建设开发wordpress列表分页 js
  • 商务通代码是不是只要放在网站根目录下就可以了域名注册需要多久
  • 做期货看什么网站东莞网站建设推广有哪些
  • 辽宁响应式网站费用湖北皇奥建设工程有限公司网站
  • 菜单 标签 wordpressseo自己怎么做
  • 网站设计需要什么免费室内设计师
  • wap网站开发联系电话设计公司网站需要多少钱
  • 定制网站收费泰州网站制作案例
  • 17做网店网站池尾电子商务网站建设个人总结
  • 网站开发前端与后端源代码电子商务网站开发的形式有
  • 5000元网站seo推广网页被禁止浏览怎么解决
  • 北京制作网站公司哪家好展架设计在哪个网站做
  • 手机网站建设机构html5 wap 网站模板
  • 网站右侧 回到顶部君和网站建设
  • 网站开发常见面试题做网站怎样赚到钱
  • 威海德嬴网站建设网站建站网站jp586 vip
  • 天河网站设计免费公司起名字大全
  • 保定企业网站建设tp框架做商城网站怎么用缓存
  • 公司网站建设企业网站微信小程序怎么推广
  • 新型城镇化建设网站中药网站模板
  • 网站 留言 以邮件形式顺德建设网站公司