码迷,mamicode.com
首页 > Web开发 > 详细

上传文件

时间:2019-01-10 10:44:21      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:调用   err   contex   value   save   enc   web   charset   import   

一  form表单上传文件

前端

 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;
    }
    
    
}

 

后端servlet

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上传文件

前端

 
    ajax上传文件  <br/>
    <form id="uploadFile">
        <input type="file" name="myPic" id="myPic"> 
        <input type="button" value="上传" id="uploadPic">
    </form>

实体类与上面相同

后端servlet

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!