当前位置:编程学习 > JSP >>

在jsp中,通过file文本得到图片信息,怎么将图片上传至服务器,你这个是怎么实现 的,可以分享一下吗,谢谢

答案:这是jsp页面 <%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; request.setAttribute("basePath", basePath); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gbk"> <title>北京缔凡思信息技术有限公司</title> <link type="text/css" rel="stylesheet" href="${basePath}backStage/css/back.css"/> <script type="text/javascript"> function messages(){ var title=document.messForm.title.value; var messageContext=document.messForm.messageContext.value; var date=document.messForm.date.value; var file=document.messForm.file.value; if(title==""){ alert("新闻主题不能为空"); document.messForm.title.focus(); return false; } if(messageContext==""){ alert("新闻内容不能为空"); document.messForm.messageContext.focus(); return false; } if(date==""){ alert("新闻发布时间不能为空"); document.messForm.date.focus(); return false; } if(file==""){ alert("新闻图片不能为空"); document.messForm.file.focus(); return false; } return true; } </script> </head> <body> <div id="rigth" style="margin-left: 250px;margin-top: 20px;"> <form action="${basePath}addMess" name="messForm" method="post" enctype="multipart/form-data" > <table cellpadding="0" cellspacing="0" width="70%"> <tr style="height: 32px; font-size: 16px;"> <th colspan="2">发布最新新闻</th> </tr> <tr> <th width="150">新闻主题</th> <td width="320"> <textarea name="title" class="textarea" rows="3" cols="40"></textarea> </td> </tr> <tr> <th>新闻图片</th> <td><input type="file" name="file" style="margin-left: 25px; width:260px; border: 1px;"/></td> </tr> <tr><td colspan="2"> <input class="res" type="reset" value="取消" onclick="window.location.href='${basePath}showMessage';"/> <input class="sub" disabled="disabled" type="submit" value="发布" onclick="return messages();"/> </td> </tr> </table> </form> </div> </body> </html> 然后在servlet中处理: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gbk"); request.setCharacterEncoding("gbk"); SmartUpload su =new SmartUpload(); su.initialize(this.getServletConfig(), request, response); String realPath = this.getServletContext().getRealPath(""); String path=realPath+"/foreStage/images";//你把图片上传至服务器中的项目的位置,foreStage是我在webContent下的文件夹 su.setAllowedFilesList("bmp,jpg,jpeg,png,gif"); try { su.upload(); su.save(path);//将文件上传至服务器指定的位置 } catch (SmartUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } String filename=su.getFiles().getFile(0).getFileName();//获取上传图片的名字 String title=su.getRequest().getParameter("title");//注意这是接收表单传过来的参数,如果用request.getParameter("title"),值为空,因为jsp把其他的文本给屏蔽掉了 } 我是这样做的,也成功了
其他:form中添加enctype="multipart/form-data",后台怎么接,得看你用的什么框架了。 建议用COS组件上传,网上有的,详细找找吧 

上一个:网上dreamweaver和jsp动态网站制作资料很少,几乎没有
下一个:JSP页面或者是html页面,怎样对图片禁止右击另存为的功能。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,