java 上传2(使用java组件fileupload和uploadify)
项目关键包和插件
插件地址:http://www.uploadify.com/documentation
项目结构
uploadify.css设置了cancel的图片路径这里修改一下
uploadify.jsp
| <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!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"> <base href="<%=basePath%>"> <title>uploadify</title> <link href="<%=basePath%>css/uploadify.css" rel="stylesheet"type="text/css"/> <style type="text/css"> <script type="text/javascript" src="<%=basePath%>/js/jquery-1.6.2.js"></script> // http://www.uploadify.com/documentation/ //The maximum number of files you are allowed to upload. When this number is reached or exceeded, the <onUploadError> event is triggered.Default Value999 //Default Value'*.*' (i.e. ‘*.jpg; *.png; *.gif’). errorMsg 完整的错误信息,如果你不重写默认的事件处理器,可以使用‘this.queueData.errorMsg’ 存取完整的错误信息 </script> </head> <body> <input type="text" name="xxxxxxxxx" /> <div id="some_file_queue"></div> <div id="pregress"></div> |
后台程序
| @RequestMapping(value="unloadify") public String unloadify(HttpServletRequest request,HttpServletResponse response){ return "/unloadify"; } @RequestMapping(value="unloadifyU",method=RequestMethod.POST) public void unloadifyU(HttpServletRequest request,HttpServletResponse response) throws FileUploadException, Exception{ Map<String,String> map = new HashMap<String,String>(); PrintWriter out = response.getWriter(); //这样获取不到unloadify中设置formData的值,要通过下面方法取(getFieldName,getString),和form中name的值(因为不是form提交) String id=request.getParameter("id"); String idd=request.getParameter("idd"); String someOtherKey=request.getParameter("someOtherKey"); String xxxxxxxxx=request.getParameter("xxxxxxxxx"); File uploadPath = new File("D:\\temp");//上传文件目录 if (!uploadPath.exists()) { uploadPath.mkdirs(); } // 临时文件目录 File tempPathFile = new File("d:\\temp\\buffer\\"); if (!tempPathFile.exists()) { tempPathFile.mkdirs(); } try { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb factory.setRepository(tempPathFile);//设置缓冲区目 ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(41943040); // 设置最大文件尺寸,这里是40MB List<FileItem> items = upload.parseRequest(request);//得到所有的文件 Iterator<FileItem> i = items.iterator(); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); if(fi.isFormField()){//如果是普通的表单字段 //获取unloadify中设置formData的值,而不是form的值,因为不是form提交所以xxxxxxxxx读取不到 String fieldName = fi.getFieldName(); System.out.println(" name is:" + fi.getFieldName()+"-value:"+ fi.getString());//显示表单内容。 }else{//如果是上传文件,显示文件名。 String fileName = fi.getName(); if (fileName != null) { //检查文件后缀格式 String fileEnd = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase(); List<String> extList=new ArrayList<String>();//ext后缀校验 extList.add("sql"); extList.add("txt"); if(extList.contains(fileEnd)){ File fullFile = new File(fi.getName()); File savedFile = new File(uploadPath, fullFile.getName()); fi.write(savedFile); }else{ map.put("res", "1"); System.out.print("ext error"); } } } } map.put("res", "0"); System.out.print("upload succeed"); } catch (Exception e) { e.printStackTrace(); } JSONObject jsonObject = JSONObject.fromObject(map); |
java 上传2(使用java组件fileupload和uploadify)的更多相关文章
- 在C#客户端用HTTP上传文件到Java服务器
在C#客户端用HTTP上传文件到Java服务器 来源:http://www.cnblogs.com/AndyDai/p/5135294.html 最近在做C / S 开发,需要在C#客户端上传文件到 ...
- java上传excel文件及解析
java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...
- edtftpj让Java上传FTP文件支持断点续传
在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传:或者想做类似迅雷下载类似的功能,文件太大,今天传一 ...
- Java上传文件FTP服务器代码
1. 在实际的应用重,通常是通过程序来进行文件的上传. 2. 实现java上传文件到ftp服务器中 新建maven项目 添加依赖 <dependency> <groupId>c ...
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...
- java 上传文件到 ftp 服务器
1. java 上传文件到 ftp 服务器 package com.taotao.common.utils; import java.io.File; import java.io.FileInpu ...
- 根据短链生成二维码并上传七牛云(Java)
通过短链生成二维码并上传七牛云(Java) 前言 网上这种帖子其实也是很多,大部分搜出来的是CSDN的,然后点进去一看都几乎一样:所以这次给个自己实践的例子记录. 这次也是通过搜索得到的一部分能实现这 ...
- java上传组件FileUpload
如果表单中有文件要上传,也就是有<input type="file" name="name"/> 就需要在form标签中添加enctype=&quo ...
- java 上传1(使用java组件fileupload)
使用fileupload要添加以下包
随机推荐
- HTML基础学习(二)—CSS
一.CSS概述 CSS(Cascading Stytle Sheets)层叠样式表,用来定义网页的显示效果.可以解决HTNL代码对样式定义的重复,提高了后期样式代码的可维护性,并增强了网页的显 ...
- Java ClassLoader加载机制
一.体系结构(自上向下) 1.Bootstrap ClassLoader(BootStrapClassLoader) --- 启动类加载器或者叫引导类加载器,加载jdk核心的APIs,这些APIs一般 ...
- jmeter JDBC Request (查询数据库获取数据库数据) 的使用
JDBC Request 这个Sampler可以向数据库发送一个jdbc请求(sql语句),并获取返回的数据库数据进行操作.它经常需要和JDBC Connection Configuration配置原 ...
- 一文搞定FastDFS分布式文件系统配置与部署
Ubuntu下FastDFS分布式文件系统配置与部署 白宁超 2017年4月15日09:11:52 摘要: FastDFS是一个开源的轻量级分布式文件系统,功能包括:文件存储.文件同步.文件访问(文件 ...
- CTF 字符统计1
题目地址:http://sec.hdu.edu.cn/question/web/1047/ 题目如下: 给你2秒钟的时间,告诉我下面这坨字符中有多少个s,多少个e,多少个c,多少个l,多少个a和多少个 ...
- NPOI操作类
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- 如何禁止火狐onblur时alert()产生类似选中的拖蓝效果
输入框中onblur 然后alert();会产生 复制 选中的效果的效果( 拖蓝) onblur="aa()"function aa(){ alert("--" ...
- Go 并发随机打印1-n
package main import ( "fmt" "math/rand" "sync" "t ...
- 微信开发(2)---微信小程序开发实战part1
微信开发现在来说,简单可以概括为两部分,微信公众号和微信小程序. 微信公众号的技术已经非常成熟.分为服务号和订阅号.简单的.可以弄一个个人订阅号,在编辑模式下就可以实现推送图文.自动回复.自定义菜单的 ...
- C语言学习第四章
今天学习C语言循环结构,为什么要用循环呢?因为有时候我们对一堆的数字进行重复的处理的时候要重复的编写一些相同或者差不多的代码,让程序显得很臃肿,而且写着也麻烦,如果用循环来写的话能简化很多,出错的话也 ...