利用uploadservlet
代码
package com.oracle.web.product; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.tomcat.util.http.fileupload.IOUtils; public class UploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 创建磁盘文件项工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); // 创建文件上传核心对象 ServletFileUpload upload = new ServletFileUpload(factory); // 解析request获得文件项对象集合 List<FileItem> parseRequest = upload.parseRequest(request); for (FileItem item : parseRequest) { // 判断是否是普通表单项 boolean formField = item.isFormField(); if (formField) { // 普通表单项获得表单的数据 封装到Product实体中 String fieldName = item.getFieldName(); // 解决普通项的乱码 String fieldValue = item.getString("UTF-8"); System.out.println(fieldName+"..."+fieldValue); } else { // 文件上传项 获得文件名称 获得文件的内容 String fileName = item.getName(); System.out.println(fileName); fileName=new String(fileName.getBytes("GBK"),"UTF-8"); System.out.println(fileName); String path = this.getServletContext().getRealPath("/upload"); File file = new File(path); // 判断上传文件的保存目录是否存在 if (!file.exists() && !file.isDirectory()) { // 创建目录 file.mkdirs(); } //文件备份到/本地项目路径 String path2 = "C:\\Users\\Administrator\\Desktop\\Marke\\WebContent\\upload"; File file2 = new File(path2); // 判断上传文件的保存目录是否存在 if (!file2.exists() && !file2.isDirectory()) { // 创建目录 file2.mkdirs(); } //上传到服务器 InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(path + "\\" + fileName); IOUtils.copy(in, out); //备份到本地项目路径 InputStream in2 = item.getInputStream(); OutputStream out2 = new FileOutputStream(path2 + "\\" + fileName); IOUtils.copy(in2, out2); in.close(); out.close(); item.delete(); in2.close(); out2.close(); response.getWriter().write("{\"path\":\"upload/"+fileName+"\"}"); } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
利用layui的上传
1.现因文件
<link rel="css/layui.css"> <script src=layui.all.js"></script> <script src=jquery-1.11.3.min.js"></script>
上传文件
<label class="layui-form-label">商品图片</label> <button type="button" class="layui-btn" id="test1"> </button> <input type="hidden" id="pimg" name="pimage" value=""> <img id="image" src=""> //js代码 layui.use(‘upload‘, function(){ var upload = layui.upload; //执行实例 var uploadInst = upload.render({ elem: ‘#test1‘ //绑定元素 ,url: ‘${pageContext.request.contextPath }/UploadServlet‘ //上传接口 ,done: function(res){ //上传完毕回调 alert(res.path); $("#pimg").val(res.path); $("#imge").prop("src",res.path); } ,error: function(){ //请求异常回调 } }); });