$.ajaxFileUpload用于文件的上传.
图片上传:
//上传图片 $("#buttonUpload").click(function(){ $.ajaxFileUpload({ url:basepath + 'ImageUpload', //调用方法 secureuri:false, fileElementId:'actImageUp', //界面id dataType: 'json', success: function (data, status) { if (data.state == "SUCCESS") { // 上传图片成功 //$("#noticePic").val(data.src); $("#noticePic").val(data.imageName); } else { $('#result').html('上传图片失败, 失败原因:'+data.state); } }, error: function (data, status, e) { $('#result').html('上传图片失败'); } }); });
jsp:
加入:
<script type="text/javascript" src="<%=basePath%>/PUBLISH/scripts/jquery.ajaxfileupload.js"></script>
<div id="result" style="FONT:12px 宋体"></div> <input type="file" name="actImageUp" id="actImageUp" style="width: 63px;"/> <input name=noticePic id="noticePic" readonly="readonly" style="width:133px;" ></input>
servelt:
package com.fjxhx.business.notice.servlet; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; public class ImageUpload extends HttpServlet { public ImageUpload() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); String realDir = request.getSession().getServletContext().getRealPath( ""); String contextpath = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + contextpath + "/"; try { String filePath = "../uploadfiles"; String realPath = realDir + "/" + filePath; System.out.println(realPath); //判断路径是否存在,不存在则创建 File dir = new File(realPath); if (!dir.isDirectory()) dir.mkdir(); if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory dff = new DiskFileItemFactory(); dff.setRepository(dir); dff.setSizeThreshold(1024000); ServletFileUpload sfu = new ServletFileUpload(dff); FileItemIterator fii = null; fii = sfu.getItemIterator(request); String title = ""; //图片标题 String url = ""; //图片地址 String fileName = ""; String state = "SUCCESS"; String realFileName = ""; while (fii.hasNext()) { FileItemStream fis = fii.next(); try { if (!fis.isFormField() && fis.getName().length() > 0) { fileName = fis.getName(); Pattern reg = Pattern .compile("[.]jpg|png|jpeg|gif$"); Matcher matcher = reg.matcher(fileName); if (!matcher.find()) { state = "请上传:jpg/png/jpeg/gif等类型图片!"; break; } realFileName = new Date().getTime() + fileName.substring(fileName .lastIndexOf("."), fileName .length()); url = realPath + "/" + realFileName; BufferedInputStream in = new BufferedInputStream( fis.openStream());//获得文件输入流 FileOutputStream a = new FileOutputStream(new File( url)); BufferedOutputStream output = new BufferedOutputStream( a); Streams.copy(in, output, true);//开始把文件写到你指定的上传文件夹 } else { String fname = fis.getFieldName(); if (fname.indexOf("pictitle") != -1) { BufferedInputStream in = new BufferedInputStream( fis.openStream()); byte c[] = new byte[10]; int n = 0; while ((n = in.read(c)) != -1) { title = new String(c, 0, n); break; } } } } catch (Exception e) { e.printStackTrace(); } } response.setStatus(200); String retxt = "{imageName:'"+realFileName+"',src:'" + basePath + filePath + "/" + realFileName + "', state:'"+state+"'}"; response.getWriter().print(retxt); } } catch (Exception ee) { ee.printStackTrace(); } } public void init() throws ServletException { // Put your code here } }
导入js文件:
相关推荐
ajaxfileupload.js用于文件上传
此为前端进行文件上传,使用Ajax方式提交的js插件,使用方便简洁,开发很高效。
AJAXFileUpload ajax 异步文件上传 进度条AJAXFileUpload ajax 异步文件上传 进度条
在使用easyUI做前端样式展示时,遇到了文件上传的问题,而且是在弹出层中提交表单,想做到不刷新页面,所以选择了使用ajaxFileUpload插件。提交表单时一直发现后台接收不到文件,后检查发现,原来...$.ajaxFileUpload({
NULL 博文链接:https://chexiazi.iteye.com/blog/2329638
AjaxFileUpload实现文件异步上传(功能... $.ajaxFileUpload({ url:"/form/file_parse.htm", secureuri:false, fileElementName:'upload-excel-file', dataType: 'json', success: function(data, status){
包中描述了多种上传文件的方式,以及使用js控件进行上传。里面含有上传文件 commons-fileupload-1.1.jar jquery.ajaxfileupload.js 以及demo。
Zepto.ajax文件上传 zepto 的文件上传库 如何使用 Zepto.ajaxFileUpload(settings); settings为上传参数,除Zepto默认设置外,需要以下元素 String fileElementId:要上传的输入元素的id 数组数据:要上传的数据,...
ajaxfileupload AJAX文件上传
用ajax的ajaxfileupload.js插件上传文件发现不支持IE9, 后来改了文件里的一些代码后可以了。 就拿出来共享。。。
ajaxfileupload单文件上传,
$.ajaxFileUpload({ url: base+'/user/upload/picture.shtml', //用于文件上传的服务器端请求地址 type: 'post', secureuri: false, //是否需要安全协议,一般设置为false contentType : "application/x-...
jQuery.AjaxFileUpload.js, jQuery插件神奇地让文件输入通过ajax上传 这个插件上传文件 somehwere,并将响应传递给回调,无其他。它不依赖于特定的HTML,只需给它一个 <input type="file">它不要求你的服务器...
NULL 博文链接:https://wangjinlongaisong-126-com.iteye.com/blog/2256622
1、jquery异步上传图片 2、js前端异步上传图片 3、下载免费 4、功能已测试可以使用