求大神帮忙servlet接收上传图片的问题 servlet图片上传 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class UploadServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked") public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // step1,创建一个DiskFileItemFactory对象 // 为解析器提供解析时的缺省的配置。 DiskFileItemFactory dfif = new DiskFileItemFactory(); // step2,创建一个解析器 ServletFileUpload sfu = new ServletFileUpload(dfif); // step3,使用解析器解析 try { // FileItem对象封装了一个表单域当中的所有的 // 数据。 List<FileItem> items = sfu.parseRequest(request); for (int i = 0; i < items.size(); i++) { FileItem item = items.get(i); // 要判断是一个普通的表单域还是 // 上传文件域 if (item.isFormField()) { // 是一个普通的表单域 String name = item.getFieldName(); String value = item.getString(); System.out.println(name + ":" + value); } else { // 上传文件域,要将文件保存在服务器端 ServletContext sc = this.getServletContext(); // 获得实际部署时物理路径 String path = sc.getRealPath("upload"); System.out.println(path); // 获得上传文件的名称 String fileName = item.getName(); System.out.println(fileName); File file = new File(path + "//" + fileName); System.out.println(file); item.write(file); } } } catch (Exception e) { e.printStackTrace(); } }}这能看明白么? 这是前台表单<form action="upload" method="post" enctype="multipart/form-data"> 姓名:<input name="name"> <br/> 上传文件:<input type="file" name="upload"> <br /> <input type="submit" value="提交" /> </form> http://blog.csdn.net/chenghui0317/article/details/9502143 谢谢 基本看明白了 加了 common-io.jar common-upload.jar 两个包就能用了 谢谢 基本看明白了 加了 common-io.jar common-upload.jar 两个包就能用了我的失误,忘跟你说引jar包了 紧急求助,关于截取字符 用超级链接打开的页面可以默认居中吗?里面的参数怎么写? 正常的项目开发时文章是保存在数据库好还是保存成txt文件好? 金融软件开发和对日外包,哪个发展前景更好? 菜鸟问题:关于服务器区别 servlet面试题 如何配置JSTL 请问如何设置JBuilder支持JBoss4.x? 求 权限管理代码 用STRUTS+HIBERNATE做的? JMS弱智问题~ 通过httpclient post方法访问struts2的action,找不到指定的action 新手学习 SPring ,Spring3.2.4里哪些Jar包需要导入
public class UploadServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked")
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// step1,创建一个DiskFileItemFactory对象
// 为解析器提供解析时的缺省的配置。
DiskFileItemFactory dfif = new DiskFileItemFactory();
// step2,创建一个解析器
ServletFileUpload sfu = new ServletFileUpload(dfif);
// step3,使用解析器解析
try {
// FileItem对象封装了一个表单域当中的所有的
// 数据。 List<FileItem> items = sfu.parseRequest(request);
for (int i = 0; i < items.size(); i++) {
FileItem item = items.get(i);
// 要判断是一个普通的表单域还是
// 上传文件域
if (item.isFormField()) { // 是一个普通的表单域
String name = item.getFieldName();
String value = item.getString();
System.out.println(name + ":" + value);
} else {
// 上传文件域,要将文件保存在服务器端
ServletContext sc = this.getServletContext(); // 获得实际部署时物理路径
String path = sc.getRealPath("upload");
System.out.println(path);
// 获得上传文件的名称
String fileName = item.getName();
System.out.println(fileName);
File file = new File(path + "//" + fileName);
System.out.println(file);
item.write(file);
}
} } catch (Exception e) {
e.printStackTrace(); }
}}
这能看明白么?
<form action="upload" method="post" enctype="multipart/form-data">
姓名:<input name="name">
<br/>
上传文件:<input type="file" name="upload">
<br />
<input type="submit" value="提交" />
</form>
common-io.jar
common-upload.jar
两个包就能用了
common-io.jar
common-upload.jar
两个包就能用了
我的失误,忘跟你说引jar包了