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

福建住房城乡建设厅网站h5制作平台排名

福建住房城乡建设厅网站,h5制作平台排名,国外引流推广软件,坚持文章目录 一、文件上传1.1 导入pom依赖1.2 配置文件上传解析器1.3 设置文件上传表单1.4 实现文件上传 二、文件下载三、多文件上传四、JRebel的使用 一、文件上传 1.1 导入pom依赖 commons-fileupload.version1.3.3/commons-fileupload.versiondependency… 文章目录 一、文件上传1.1 导入pom依赖1.2 配置文件上传解析器1.3 设置文件上传表单1.4 实现文件上传 二、文件下载三、多文件上传四、JRebel的使用 一、文件上传 1.1 导入pom依赖 commons-fileupload.version1.3.3/commons-fileupload.versiondependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion${commons-fileupload.version}/version/dependency1.2 配置文件上传解析器 在spring-mvc.xml文件中添加文件上传解析器。 bean idmultipartResolver classorg.springframework.web.multipart.commons.CommonsMultipartResolver!-- 必须和用户JSP 的pageEncoding属性一致以便正确解析表单的内容 --property namedefaultEncoding valueUTF-8/property!-- 文件最大大小(字节) 1024*1024*5050M--property namemaxUploadSize value52428800/property!--resolveLazily属性启用是为了推迟文件解析以便捕获文件大小异常--property nameresolveLazily valuetrue/ /bean这段代码是一个Spring框架的配置用于处理文件上传功能。它定义了一个名为multipartResolver的Bean使用org.springframework.web.multipart.commons.CommonsMultipartResolver类来处理文件上传。其中设置了默认的编码方式为UTF-8文件的最大大小为50MB并启用了懒解析模式。这段代码的作用是配置文件上传的相关参数。 1.3 设置文件上传表单 % page contentTypetext/html;charsetUTF-8 languagejava % html headbase href${pageContext.request.contextPath }title文件上传/title /head body form action/file/upload methodpost enctypemultipart/form-datalabel编号/labelinput typetext nameid readonlyreadonly value${param.id}/br/label图片/labelinput typefile nameimgFile/br/input typesubmit value上传图片/ /form /body /html1.4 实现文件上传 1 设计表 (2) 配置generatorConfig.xml并生成代码 table schema tableNamefile_upload domainObjectNameUploadFileenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/table 3 创建业务层 4 配置resource.properties #本地路径 dirD:/upload/ #服务器路径 server/upload/(5) 配置文件读取工具类 package com.xqx.utils;import java.io.IOException; import java.io.InputStream; import java.util.Properties;public class PropertiesUtil {public static String getValue(String key) throws IOException {Properties p new Properties();InputStream in PropertiesUtil.class.getResourceAsStream(/resource.properties);p.load(in);return p.getProperty(key);} }6 配置项目与映射地址 (7) 编写控制器 package com.xqx.web;import com.xqx.biz.UploadFileBiz; import com.xqx.model.UploadFile; import com.xqx.utils.PageBean; import com.xqx.utils.PropertiesUtil; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.List;Controller RequestMapping(/file) public class UploadFileController {Autowiredprivate UploadFileBiz UploadFileBiz;/*新增方法*/RequestMapping(/add)public String save(UploadFile UploadFile, HttpServletRequest request) {UploadFileBiz.insertSelective(UploadFile);return redirect:list;}/*删除方法*/RequestMapping(/del/{id})public String del(PathVariable(id) Integer id) {UploadFileBiz.deleteByPrimaryKey(id);return redirect:/file/list;}/*修改方法*/RequestMapping(/edit)public String edit(UploadFile UploadFile, HttpServletRequest request) {UploadFileBiz.updateByPrimaryKeySelective(UploadFile);return redirect:list;}/*查询方法*/RequestMapping(/list)public String list(UploadFile UploadFile, HttpServletRequest request) {PageBean pageBean new PageBean();pageBean.setRequest(request);ListUploadFile UploadFiles UploadFileBiz.listPager(UploadFile, pageBean); // ModelAndView modelAndView new ModelAndView(); // modelAndView.addObject(UploadFiles, UploadFiles); // modelAndView.addObject(pageBean, pageBean); // modelAndView.setViewName(UploadFile/list);request.setAttribute(UploadFiles, UploadFiles);request.setAttribute(pageBean, pageBean);return file/list;}/*数据回显*/RequestMapping(/preSave)public String preSave(UploadFile UploadFile, HttpServletRequest request) {if (UploadFile ! null UploadFile.getId() ! null UploadFile.getId() ! 0) {UploadFile img UploadFileBiz.selectByPrimaryKey(UploadFile.getId());request.setAttribute(img, img);}return file/edit;}/*图片上传*/RequestMapping(upload)public String upload(UploadFile img,MultipartFile imgFile) throws IOException {//读取配置文夹本地路径和服务器路径String dir PropertiesUtil.getValue(dir);String server PropertiesUtil.getValue(server);//利用MultipartFile类接受前端传递到后台的文件System.out.println(文件名imgFile.getOriginalFilename());System.out.println(文件类型imgFile.getContentType());//将文件转成流写入到服务器FileUtils.copyInputStreamToFile(imgFile.getInputStream(),new File(dirimgFile.getOriginalFilename()));//通过对象将图片保存到数据库img.setImg(serverimgFile.getOriginalFilename());UploadFileBiz.updateByPrimaryKeySelective(img);return redirect:list;} }8 编写jsp % page contentTypetext/html;charsetUTF-8 languagejava % % taglib prefixc urihttp://java.sun.com/jsp/jstl/core % % taglib prefixw urihttp://jsp.xqx.cn % html headmeta http-equivContent-Type contenttext/html; charsetUTF-8linkhrefhttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.cssrelstylesheetscriptsrchttps://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js/scriptbase href${pageContext.request.contextPath }title/titlestyle typetext/css.page-item input {padding: 0;width: 40px;height: 100%;text-align: center;margin: 0 6px;}.page-item input, .page-item b {line-height: 38px;float: left;font-weight: 400;}.page-item.go-input {margin: 0 10px;}/style /head body form classform-inlineaction/file/list methodpostdiv classform-group mb-2input typetext classform-control-plaintext namenameplaceholder请输入用户名称/divbutton typesubmit classbtn btn-primary mb-2查询/buttona classbtn btn-primary mb-2 href/file/preSave新增/a /formtable classtable table-stripedtheadtrth scopecolID/thth scopecol用户/thth scopecol图片/th/tr/theadtbodyc:forEach vari items${uploadImgs }trtd${i.id }/tdtd${i.name }/tdtdimg src${i.img } stylewidth: 200px;height: 100px;/tdtda href/file/preSave?id${i.id}修改/aa href/file/del/${i.id}删除/aa href/page/file/upload?id${i.id}图片上传/aa href/file/download?id${i.id}图片下载/a/td/tr/c:forEach/tbody /table !-- 这一行代码就相当于前面分页需求前端的几十行了 -- w:page pageBean${pageBean }/w:page/body /html二、文件下载 根据传入的文件id查询对应的文件信息然后根据文件路径读取文件内容并将文件内容和设置好的HTTP头信息封装成一个ResponseEntity对象最后返回给客户端进行文件下载。 RequestMapping(/download)public ResponseEntitybyte[] download(UploadImg uploadImg, HttpServletRequest req){try {//先根据文件id查询对应图片信息UploadImg img this.uploadImgBiz.selectByPrimaryKey(uploadImg.getId());String diskPath PropertiesUtil.getValue(dir);String reqPath PropertiesUtil.getValue(server);//上面获取的数据库地址需要转换才能下载成本地路径String realPath img.getImg().replace(reqPath,diskPath);String fileName realPath.substring(realPath.lastIndexOf(/)1);//下载关键代码File filenew File(realPath);HttpHeaders headers new HttpHeaders();//http头信息String downloadFileName new String(fileName.getBytes(UTF-8),iso-8859-1);//设置编码headers.setContentDispositionFormData(attachment, downloadFileName);headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//MediaType:互联网媒介类型 contentType具体请求中的媒体类型信息return new ResponseEntitybyte[](FileUtils.readFileToByteArray(file),headers, HttpStatus.OK);}catch (Exception e){e.printStackTrace();}return null;}三、多文件上传 //多文件上传RequestMapping(/uploads)public String uploads(HttpServletRequest req, Music music, MultipartFile[] files){try {StringBuffer sb new StringBuffer();for (MultipartFile cfile : files) {//思路//1) 将上传图片保存到服务器中的指定位置String dir PropertiesUtil.getValue(dir);String server PropertiesUtil.getValue(server);String filename cfile.getOriginalFilename();FileUtils.copyInputStreamToFile(cfile.getInputStream(),new File(dirfilename));sb.append(filename).append(,);}System.out.println(sb.toString());} catch (Exception e) {e.printStackTrace();}return redirect:list;}四、JRebel的使用 下载
http://www.zqtcl.cn/news/232788/

相关文章:

  • 泉州模板建站定制成都网页设计培训机构
  • 个人微信公共号可以做微网站么免费产品推广软件
  • 建设银行瓶窑支行网站阿里域名官网
  • 宿迁网站seo中原建设信息网 网站
  • 地方网站域名用全拼建设银行网站怎么登录密码忘了怎么办
  • win7 iis7 添加网站秦皇岛 网站建设
  • 手机模板网站模板下载工具Wordpress elgg
  • 宠物网站建设的目的wordpress图创
  • 网站首页图片怎么更换浙江省建设政务网站
  • 宁波有哪家公司做网站的京东联盟网站建设电脑版
  • 电商网站业务流程网站制作在哪找
  • 学校网站建设教程加盟网站制作费用
  • fqapps网站建设少儿戏曲知识 网站建设
  • 产品网站建设框架wordpress用户名密码加密方式
  • 入侵dedecms网站管理员密码青岛seo整站优化公司
  • 小网站备案南宁网站建设排名
  • 西安免费做网站wordpress 使用方法
  • 企业营销的意义优化核心系列网站
  • 微信网站设计一起做网站17广州
  • 重庆网络推广网站如何制作app演示视频
  • 网站logo是指手机上做app的软件
  • 做母婴育儿类网站好做seo排名吗深圳网站. 方维网络
  • 小型装修公司店面装修windows优化大师会员
  • php服装商城网站建设wordpress主题去除友情链接
  • 北京网站设计公司sx成都柚米科技15福建众利建设工程网站
  • 深圳大型网站建设服务公司wordpress后台为什么这么慢
  • 信用网站建设工作简报青岛的建筑公司
  • 网站怎么做文件上传灯饰 东莞网站建设
  • 建设电子商务网站的规划书电子商务平台网站模板
  • 桂林网站建设 腾云安康养老院收费