我是靠谱客的博主 欣慰皮卡丘,最近开发中收集的这篇文章主要介绍JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等FORM里设置了enctype="multipart/form-data"后用不能利用struts把数据传到后台action中,因为是流传输方式。,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
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>
也许用其他方式也可以解决这个问题,但个人觉得这种方式较简单。
最后
以上就是欣慰皮卡丘为你收集整理的JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等FORM里设置了enctype="multipart/form-data"后用不能利用struts把数据传到后台action中,因为是流传输方式。的全部内容,希望文章能够帮你解决JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等FORM里设置了enctype="multipart/form-data"后用不能利用struts把数据传到后台action中,因为是流传输方式。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复