标签:调用 err contex value save enc web charset import
form表单上传文件 <form method="post" enctype="multipart/form-data" action="saveUser.do"> 用户名<input type="text" name="username"><br/> 头像<input type="file" name="myPic"><br/> <input type="submit" > </form>
package com.qy.entity; public class User { private String username; private String myPic; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getMyPic() { return myPic; } public void setMyPic(String myPic) { this.myPic = myPic; } }
package com.qy.servlet; import java.io.IOException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import com.qy.entity.User; @WebServlet("/saveUser.do") @MultipartConfig public class UserSaveServlet extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); Part part = request.getPart("myPic"); System.out.println(part); //验证文件大小,后缀是否满足要求,获取原始文件名 long size = part.getSize(); //1m 1024*1024 if(size > 1024*1024){ System.out.println("文件大于1M!"); return ; } // String file = part.getHeader("Content-Disposition");//form-data; name="myPic"; filename="1024.jpg" //获取后缀 截取 . String suffix = file.substring(file.lastIndexOf(".")+1, file.length()-1); //比对后缀格式是否正确 String[] suffixs = {"jpg","png","gif"};content.lastIndexOf("=\"")+2,content.lastIndexOf("\"") boolean flag = Arrays.asList(suffixs).contains(suffix); if(!flag){ System.out.println("文件格式不正确!"); return ; } //获取保存路径 String path = request.getServletContext().getRealPath("/images/"); //保存文件到磁盘 String filename = System.currentTimeMillis()+"."+suffix; part.write(path+filename); //保存用户数据到数据库 User user = new User(); user.setUsername(username); user.setMyPic("images/"+filename); //调用dao层方法保存对象 //控制页面跳转 } }
ajax上传文件 <br/> <form id="uploadFile"> <input type="file" name="myPic" id="myPic"> <input type="button" value="上传" id="uploadPic"> </form>
package com.qy.servlet; import java.io.IOException; import java.util.Arrays; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; import com.qy.entity.User; @WebServlet("/saveUser2.do") @MultipartConfig public class UserSaveServlet2 extends HttpServlet{ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); Part part = request.getPart("myPic"); //验证文件大小,后缀是否满足要求,获取原始文件名 long size = part.getSize(); //1m 1024*1024 if(size > 1024*1024){ System.out.println("文件大于1M!"); response.getWriter().append("文件大于1M!"); return ; } // String file = part.getHeader("Content-Disposition");//form-data; name="myPic"; filename="1024.jpg" //获取后缀 截取 . String suffix = file.substring(file.lastIndexOf(".")+1, file.length()-1); //比对后缀格式是否正确 String[] suffixs = {"jpg","png","gif"}; boolean flag = Arrays.asList(suffixs).contains(suffix); if(!flag){ System.out.println("文件格式不正确!"); response.getWriter().append("文件格式不正确!"); return ; } //获取保存路径 String path = request.getServletContext().getRealPath("/images/"); //保存文件到磁盘 String filename = System.currentTimeMillis()+"."+suffix; part.write(path+filename); //保存用户数据到数据库 User user = new User(); user.setUsername(username); user.setMyPic("images/"+filename); //调用dao层方法保存对象 //返回保存成功 response.getWriter().append("上传成功!"); } }
标签:调用 err contex value save enc web charset import
原文地址:https://www.cnblogs.com/songff/p/songff1.html