原文地址:https://blog.csdn.net/niceliusir/article/details/78453560

以下是jsp+servlet和SSM分别是如何实现文件上传的方法示例

两种模式的upload.jsp文件都一样,(注意要加上enctype=”multipart/form-data”)如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body> <form action="指定控制器" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>文件:</td>
<td><input type="file" name="uploadfile"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="upload" value="上传"></td>
</tr>
</table> </form> </body>
</html>

传统的jsp+servlet开发实现上传
UploadServlet.java中的doPost()如下:

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;chartset=utf-8");
request.setCharacterEncoding("utf-8");
// 上传的用户名
String value = null;
// 上传的文件名
String fileName = null;
// 上传的目标路径
String filePath = request.getSession().getServletContext().getRealPath("/upload"); // 1.判断表单上传的编码方式
if(ServletFileUpload.isMultipartContent(request)){
// 2.创建fileItem工厂
FileItemFactory factory = new DiskFileItemFactory();
// 3.创建上传解析对象
ServletFileUpload sfu = new ServletFileUpload(factory);
// 4.解析上传的表单
List<FileItem> fileItemList = null;
try {
fileItemList = sfu.parseRequest(request);
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(FileItem f : fileItemList){
if(f.isFormField()){// 普通表单元素
String name = f.getFieldName();
if(name.equals("username")){
value = f.getString("utf-8");
}
}else{// 文件
fileName = f.getName();
File file = new File(filePath + "/" + fileName);
try {
f.write(file);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.getWriter().print(value + "上传了" + fileName + "已成功!");
} }
} }

SSM框架主要是spring MVC处理器对上传组件进行了封装,使得代码简化了很多
处理器中的处理上传文件的方法如下:

/**
* 文件上传
*TODO
*LIU
* @param request
* @param response
* @return
*上午10:34:15
*/
@RequestMapping("uploadFile.action")
public ModelAndView uploadFile(@RequestParam("uploadfile") CommonsMultipartFile cmf, HttpServletRequest request) throws Exception{ // 接收普通的用户名的话,用参数request来接收
String uname = request.getParameter("uname");
String path = "F:\\upload\\" + cmf.getOriginalFilename();
File file = new File(path); cmf.transferTo(file); mav = new ModelAndView("uploadResult.jsp");
String mess = cmf.getOriginalFilename() + "上传成功了!";
mav.addObject("mess", mess);
mav.addObject("uname", uname);
return mav;
}

【转】jsp+servlet和SSM分别是如何实现文件上传(示例)的更多相关文章

  1. HTTP文件上传插件开发文档-JSP

    版权所有 2009-2016 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-u ...

  2. jsp实现文件上传(二)用cos组件实现文件上传

    jsp表单 <%@ page language="java" pageEncoding="utf-8"%> <html> <hea ...

  3. servlet文件上传及下载

    servlet3.0中提供了对文件上传的直接支持,不需要借助任何第三方上传组件,直接使用Servlet3.0提供的API就能够实现文件上传功能. servlet 代码: package ni.jun. ...

  4. SSM框架下,使用ajax请求上传文件(doc\docx\excel\图片等)

    1.准备工作 1.1.添加上传必要jar包 <dependency> <groupId>commons-io</groupId> <artifactId> ...

  5. 使用jsp/servlet简单实现文件上传与下载

    使用JSP/Servlet简单实现文件上传与下载    通过学习黑马jsp教学视频,我学会了使用jsp与servlet简单地实现web的文件的上传与下载,首先感谢黑马.好了,下面来简单了解如何通过使用 ...

  6. 通过JSP+servlet实现文件上传功能

    在TCP/IP中,最早出现的文件上传机制是FTP.它将文件由客户端到服务器的标准机制. 但是在JSP中不能使用FTP来上传文件,这是有JSP的运行机制所决定的. 通过为表单元素设置Method=&qu ...

  7. JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)

    链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...

  8. jsp Servlet 文件上传

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. jsp使用servlet实现文件上传

    1.在index.jsp中写入以下代码 <form method="post" action="demo3" enctype="multipar ...

随机推荐

  1. SQL Server 阻止了对组件 'Agent XPs' 的 过程 'dbo.sp_set_sqlagent_properties' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。

    Sqlserver 2008 在配置分发向导的时候报了如下错误: 使用 Agent XPs 选项可以启用此服务器上的 SQL Server 代理扩展存储过程.如果禁用此选项,则 SQL Server ...

  2. 【转】Tesla Autopilot

    Tesla Autopilot 以下内容是<Tesla Model S的设计失误>一文中新加入的小节.由于写作时间相距太远,而且由于它的时效性,现在也把它单独提出来,独立成文. 两个月前, ...

  3. C++的字符串格式化库

    这里向大家介绍一个C++的字符串格式化库,叫cpptempl,这个库支持对字符串格式的条件,循环,变量插入.看上去很不错,只不过其是基于boost库的. 下面是一个例子: 1 2 3 4 5 6 7 ...

  4. nginx location 配置详解 【转载,整理】

    http://www.nginx.cn/115.html NGINX location 配置参考:http://www.cnblogs.com/zlingh/p/6288994.html https: ...

  5. Java 8 flatMap example

    Java 8 flatMap example In Java 8, Stream can hold different data types, for examples: Stream<Stri ...

  6. Gituhb 上一些值得攻读的玩具代码库

    https://github.com/sindresorhus/globby https://github.com/dylansmith/node-exif-renamer https://githu ...

  7. adb 切换android输入法

    自动化测试执行时,使用了appium输入法,再手动使用时,需要进入设置里面进行切换.adb 也提供了一条命令使用命令切换 1.找出android里面有多少输入法:adb shell ime list ...

  8. 微信小程序:冒泡事件及其阻止

    事件的类别分为几种: 点击事件:tap 长按事件:longtap 触摸事件:touchstart: touchend:touchcancel:touchmove 其他:input:submit.... ...

  9. update document in mongodb using java -摘自网络

    update document in mongodb using java: Mongodb driver provides functionality to update document in m ...

  10. oozie 常用命令

    1.验证wokflow.xmloozie validate /appcom/apps/hduser0401/mbl_webtrends/workflow.xml 2.提交作业,作业进入PREP状态 o ...