JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等
来源:互联网 发布:php超链接代码 编辑:程序博客网 时间:2024/04/30 00:39
FORM里设置了enctype="multipart/form-data"后用不能利用struts把数据传到后台action中,因为是流传输方式。
用fileupload工具包解决:
action中代码:
String root = ServletActionContext.getRequest().getRealPath("/upload");DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory);try {List items = upload.parseRequest(ServletActionContext.getRequest());Iterator it = items.iterator();while (it.hasNext()) {FileItem item = (FileItem) it.next();if (item.isFormField()) { // 如果是表单域if (item.getFieldName().equals("newstype")) {String newstype = item.getString("UTF-8");newstemp.setNewstype(Integer.parseInt(newstype));}if (item.getFieldName().equals("newsauthor")) {String newsauthor = item.getString("UTF-8");newstemp.setAuthor(newsauthor);}if (item.getFieldName().equals("newsisshowfront")) {String newsisshowfront = item.getString("UTF-8");newstemp.setIsshowfront(Integer.parseInt(newsisshowfront));}if (item.getFieldName().equals("newstitle")) {String newstitle = item.getString("UTF-8");newstemp.setNewstitle(newstitle);}if (item.getFieldName().equals("newscontent")) {String newscontent = item.getString("UTF-8");newstemp.setNewscontent(newscontent);}} else { // 如果是文件if (item.getName() != null && !item.getName().equals("")) {File file = new File(root,item.getName());newstemp.setNewsimages("upload/"+item.getName());item.write(file);}}}adminservice.addNews(newstemp);} catch (Exception e) {e.printStackTrace();System.err.println("上传文件不成功!");}JSP代码:
<input type="text" class="input-200" name="newsauthor"/><s:file name="image" label="文件"></s:file>
也许用其他方式也可以解决这个问题,但个人觉得这种方式较简单。
1 0
- JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等
- 百度ueditor富文本--图片保存路径的配置以及上传到远程服务器
- 图片上传以及保存
- 图片上传以及保存
- android上传文件到服务器(图片、文本等)
- 带图片预览功能的上传表单
- 上传图片到jsp
- 图片上传并保存到数据库以及显示图片
- UEditor图片上传保存位置的修改(JSP)
- 图片+表单的上传
- 使用DiskFileItemFactory在Action中获得JSP表单内容(用在有图片上传功能的表单中)
- 使用DiskFileItemFactory在Action中获得JSP表单内容(用在有图片上传功能的表单中)
- Retrofit2.0 带文本多图片上传及后台spring mvc 文本图片接收
- ASI上传图片到后台
- 图片、文件表单上传以及异步上传
- 如何实现图片的上传以及将图片保存到Mysql
- Java 后台实现上传图片的接收并保存
- android上传图片(及普通参数)到服务器(j2ee后台服务器,ssh框架)
- Android SurfaceView实战 打造抽奖转盘
- 交叉编译qt5.3.2
- [总结]FFMPEG视音频编解码零基础学习方法
- SQLite3移植到ARM Linux教程
- Posix多线程笔记(五)-----条件变量
- JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等
- isdeployed
- 单实例迁移到rac
- PGSQL连接数据库并对其进行模式创建和表的继承复制
- CentOS使用samba访问Windows共享文件夹
- ORM尝试一 总体思路
- 【LeetCode】 Permutations 排列生成算法之字典序法
- spring事务配置方法(BeanNameAutoProxyCreator)
- 编译poppler-qt5