discuz做资讯网站,进入公众号怎么找出二维码,网站改版 权重,公司外宣网站基于Spring MVC实现文件上传#xff1a; 使用commons-fileupload实现上传文件到本地目录。 实现上传文件到阿里云OSS和从阿里云OSS下载文件到本地。
1. 创建项目
选择Maven快速构建web项目#xff0c;项目名称为case14-springmvc03。 2. 配置Maven依赖
?xml ver…基于Spring MVC实现文件上传 使用commons-fileupload实现上传文件到本地目录。 实现上传文件到阿里云OSS和从阿里云OSS下载文件到本地。
1. 创建项目
选择Maven快速构建web项目项目名称为case14-springmvc03。 2. 配置Maven依赖
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.wfit.springmvc/groupIdartifactIdspringmvc02/artifactIdversion1.0-SNAPSHOT/versionpackagingwar/packagingpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.target/propertiesdependencies!--spring mvc--dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.3.8/version/dependency!--servlet--dependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/versionscopeprovided/scope/dependency!--fileupload--dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.4/version/dependency!--aliyun-sdk-oss--dependencygroupIdcom.aliyun.oss/groupIdartifactIdaliyun-sdk-oss/artifactIdversion3.15.1/version/dependency/dependencies
/project
3. 创建Spring MVC配置文件
src.main.resources目录下创建spring-mvc.xml。
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd!--开启注解--context:component-scan base-packagecom.wfit/!--启用mvc--mvc:annotation-driven/mvc:annotation-driven!--文件上传解析器--bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver!--设置上传大小 最大1M--property namemaxUploadSize value1048576//bean
/beans
4. 配置web.xml
!DOCTYPE web-app PUBLIC-//Sun Microsystems, Inc.//DTD Web Application 2.3//ENhttp://java.sun.com/dtd/web-app_2_3.dtd
web-app!--解决POST中文乱码问题 过滤器--filterfilter-nameencodingFilter/filter-namefilter-classorg.springframework.web.filter.CharacterEncodingFilter/filter-classinit-paramparam-nameencoding/param-nameparam-valueutf-8/param-value/init-param/filterfilter-mappingfilter-nameencodingFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping!--配置前端控制器DispatcherServlet--servletservlet-namedispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class!--加载SpringMVC文件--init-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring-mvc.xml/param-value/init-param!--启动容器时加载servlet--load-on-startup1/load-on-startup/servletservlet-mappingservlet-namedispatcherServlet/servlet-name!--表示拦截所有请求--url-pattern//url-pattern/servlet-mapping
/web-app
5. 创建UploadController类
在src.main.java.com.wfit.upload目录下创建UploadController类实现文件上传。
Controller
RequestMapping(/upload)
public class UploadController {/*** 文件上传* param file* return*/PostMapping(/upload)ResponseBodypublic String upload(MultipartFile file) throws IOException {//验证文件是否为空if(ObjectUtils.isEmpty(file) || file.getSize() 0){return file is empty;}//创建上传文件位置File uploadDir new File(D:\\upload);//获取文件名String fileName file.getOriginalFilename();//创建目标文件File desFile new File(uploadDir,fileName);//执行上传操作file.transferTo(desFile);return success;}
}
6. 创建OssController类
在src.main.java.com.wfit.upload目录下创建OssController类实现上传文件到阿里云OSS和从阿里云OSS下载文件到本地。
Controller
RequestMapping(/oss)
public class OssController {//访问OSS的域名private static String endpoint oss-cn-beijing.aliyuncs.com;//accessKeyId和accessKeySecret是OSS的访问密钥private static String accessKeyId yourAccessKeyId;private static String accessKeySecret yourAccessKeySecret;//Bucket用来管理所存储Object的存储空间private static String bucketName yourBucketName;//目标文件private static String objectName upload/123.txt;/*** 上传文件到阿里云OSS* return*/PostMapping(/upload)ResponseBodypublic String upload(MultipartFile file) throws IOException {//验证文件是否为空if(ObjectUtils.isEmpty(file) || file.getSize() 0){return file is empty;}InputStream inputStream file.getInputStream();// 创建OSSClient实例OSS ossClient new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 创建PutObjectRequest对象PutObjectRequest putObjectRequest new PutObjectRequest(bucketName, objectName, inputStream);// 设置该属性可以返回response。如果不设置则返回的response为空putObjectRequest.setProcess(true);// 创建PutObject请求PutObjectResult result ossClient.putObject(putObjectRequest);// 如果上传成功则返回200System.out.println(result.getResponse().getStatusCode());} catch (Exception e) {System.out.println(Error Message: e.getMessage());} return success;}/*** 下载阿里云文件到本地* return*/PostMapping(/download)ResponseBodypublic String download(String objName,String path) throws IOException {//下载目录String pathName download/123.txt;// 创建OSSClient实例OSS ossClient new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 下载Object到本地文件并保存到指定的本地路径中ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));} catch (Exception e) {System.out.println(Error Message: e.getMessage());}return success;}
}
7. Postman执行上传文件到本地目录 8. Postman执行上传到阿里云OSS 9. Postman执行从阿里云OSS下载文件到本地 文章转载自: http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.pftjj.cn.gov.cn.pftjj.cn http://www.morning.qwpyf.cn.gov.cn.qwpyf.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.qmbpy.cn.gov.cn.qmbpy.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.cjsnj.cn.gov.cn.cjsnj.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.plcyq.cn.gov.cn.plcyq.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.wflsk.cn.gov.cn.wflsk.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.qbwmz.cn.gov.cn.qbwmz.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.ttfh.cn.gov.cn.ttfh.cn http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.lbbyx.cn.gov.cn.lbbyx.cn http://www.morning.qkrz.cn.gov.cn.qkrz.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.bsgfl.cn.gov.cn.bsgfl.cn http://www.morning.hhpbj.cn.gov.cn.hhpbj.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.tcxzn.cn.gov.cn.tcxzn.cn http://www.morning.kmlmf.cn.gov.cn.kmlmf.cn http://www.morning.nj-ruike.cn.gov.cn.nj-ruike.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.mhmsn.cn.gov.cn.mhmsn.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.nsrlb.cn.gov.cn.nsrlb.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.mlnby.cn.gov.cn.mlnby.cn http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.lmmh.cn.gov.cn.lmmh.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.gl-group.cn.gov.cn.gl-group.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.ccpnz.cn.gov.cn.ccpnz.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.hphfy.cn.gov.cn.hphfy.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn