servlet上传图片到服务器以及Ajax异步上传

servlet上传图片到服务器以及Ajax异步上传_第1张图片

servlet上传图片到服务器以及Ajax异步上传_第2张图片

前端代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



Ajax异步上传图片
 
   




	

用于读取上传后的图片


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



" >

<%=request.getAttribute("picture") %>
helloword

servlet代码

package service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

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 org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;


 

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
@MultipartConfig  //使用MultipartConfig注解标注改servlet能够接受文件上传的请求
public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
  
    /**
     * 上传数据及保存文件
     */
    
    protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
    	
		
		Part part = request.getPart("file");
        String disposition = part.getHeader("Content-Disposition");
        if (part.getContentType().contains("image")) {//判断是否为图片		
        String suffix = disposition.substring(disposition.lastIndexOf("."),disposition.length()-1);
          //随机的生存一个32的字符串
        System.out.println(suffix);
        String filename = UUID.randomUUID()+suffix;
          //获取上传的文件名
        System.out.println(filename );
        InputStream is = part.getInputStream();
        //动态获取服务器的路径
        String serverpath = request.getServletContext().getRealPath("upload");
      
        System.out.println(serverpath);
        FileOutputStream fos = new FileOutputStream(serverpath+"/"+filename);
        byte[] bty = new byte[1024];
        int length =0;
        int i=0;
        while((length=is.read(bty))!=-1){
            fos.write(bty,0,length);
       
        }
        fos.close();
        is.close();
     
        String s=serverpath+"/"+filename;
        System.out.println(s);
        out.write(s);
   
       request.setAttribute("picture",s );
    	request.getRequestDispatcher("NewFile.jsp").forward(request, response);
    
        }
        else{
			  out.write("请选择图片文件!!!");
		}

    }
   

}

 

 

ajax异步上传图片到服务器(servlet代码不变)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



Ajax异步上传图片
 
   




	  
	

 

你可能感兴趣的:(JavaWeb,ajax,servlet)