JSP(SSH)表单上传图片以及文本内容到后台保存,上传带图片的文章新闻等

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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用HTML5的FormData对象来实现多文件上传,然后使用Ajax将数据发送到服务器。在服务器端,您可以使用Java的Servlet来处理上传的文件,并将文件路径保存到数据库中。以下是一个简单的示例代码: HTML代码: ```html <form id="myForm" enctype="multipart/form-data"> <input type="file" name="file1"> <input type="file" name="file2"> <input type="button" value="Upload" onclick="uploadFiles()"> </form> ``` JavaScript代码: ```javascript function uploadFiles() { var form = document.getElementById("myForm"); var formData = new FormData(form); var xhr = new XMLHttpRequest(); xhr.open("POST", "uploadServlet", true); xhr.onreadystatechange = function() { if (xhr.readyState == 4 && xhr.status == 200) { alert(xhr.responseText); } }; xhr.send(formData); } ``` Java代码: ```java @WebServlet("/uploadServlet") @MultipartConfig public class UploadServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String file1Path = saveFile(request.getPart("file1")); String file2Path = saveFile(request.getPart("file2")); // 将文件路径保存到数据库中 // ... response.getWriter().write("Files uploaded successfully!"); } private String saveFile(Part part) throws IOException { String fileName = getFileName(part); String filePath = "C:/uploads/" + fileName; part.write(filePath); return filePath; } private String getFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); String[] tokens = contentDisposition.split(";"); for (String token : tokens) { if (token.trim().startsWith("filename")) { return token.substring(token.indexOf("=") + 2, token.length() - 1); } } return ""; } } ``` 请注意,此示例代码仅供参考,您需要根据自己的需求进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值