多文件上传的后台代码实现
来源:互联网 发布:要怎么加盟农村淘宝 编辑:程序博客网 时间:2024/05/17 06:28
多文件上传后台代码的实现:
- /**
- * 文件上传实例
- * @author samLee
- *
- */
- public class UploadPhotoServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public UploadPhotoServlet() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- this.doPost(request, response);
- }
- @SuppressWarnings("unchecked")
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //文件存放的目录
- File tempDirPath =new File(request.getSession().getServletContext().getRealPath("/")+"\\upload\\");
- if(!tempDirPath.exists()){
- tempDirPath.mkdirs();
- }
- //创建磁盘文件工厂
- DiskFileItemFactory fac = new DiskFileItemFactory();
- //创建servlet文件上传组件
- ServletFileUpload upload = new ServletFileUpload(fac);
- //文件列表
- List fileList = null;
- //解析request从而得到前台传过来的文件
- try {
- fileList = upload.parseRequest(request);
- } catch (FileUploadException ex) {
- ex.printStackTrace();
- return;
- }
- //保存后的文件名
- String imageName = null;
- //便利从前台得到的文件列表
- Iterator<FileItem> it = fileList.iterator();
- while(it.hasNext()){
- FileItem item = it.next();
- //如果不是普通表单域,当做文件域来处理
- if(!item.isFormField()){
- imageName = new Date().getTime()+Math.random()*10000+item.getName();
- BufferedInputStream in = new BufferedInputStream(item.getInputStream());
- BufferedOutputStream out = new BufferedOutputStream(
- new FileOutputStream(new File(tempDirPath+"\\"+imageName)));
- Streams.copy(in, out, true);
- }
- }
- //
- PrintWriter out = null;
- try {
- out = encodehead(request, response);
- } catch (IOException e) {
- e.printStackTrace();
- }
- //这个地方不能少,否则前台得不到上传的结果
- out.write("1");
- out.close();
- }
- /**
- * Ajax辅助方法 获取 PrintWriter
- * @return
- * @throws IOException
- * @throws IOException
- * request.setCharacterEncoding("utf-8");
- response.setContentType("text/html; charset=utf-8");
- */
- private PrintWriter encodehead(HttpServletRequest request,HttpServletResponse response) throws IOException{
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html; charset=utf-8");
- return response.getWriter();
- }
- }
0 0
- 多文件上传的后台代码实现
- java实现文件上传和下载的后台代码
- 上传文件代码的实现
- webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)
- webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)
- 文件上传的后台
- struts多文件上传的实现描述附带代码
- HTML5实现多文件的上传示例代码
- html5实现多文件的上传示例代码
- php实现文件上传的代码
- PhoneGap 文件上传(Java 后台代码)
- Magento多文件上传代码功能实现
- php多文件上传实现代码
- php实现多文件上传代码
- php多文件上传实现代码
- Html5&Ajax实现文件后台上传
- struts2实现文件上传 代码
- Sruts2下实现多文件上传功能(后台上传app软件,做版本更新)
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- IOS自动播放音频/视频
- 迷茫的转行之路,寻求突波点一
- Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用
- UIAlertView ios9 开始废弃 使用UIAlertController弹框提示
- 多文件上传的后台代码实现
- qduoj31帅气的HYC切蛋糕
- Java基础--工具类(Collections、Arrays)及其他常用类
- xen热迁移代码执行流程相关文件
- KVC
- 筛选法求素数
- C#基础精华02(静态类,值类型,引用类型,枚举,结构,ref与out)
- Linux系统安装GCC软件的依赖关系
- 文字识别转换