java ajax上传文件图片以json形式返回

jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'ajaxtext.jsp' starting page
    
	
	
	    
	
	
	
    
  
  
  
      
>

java页面 上传图片的位置参考 https://blog.csdn.net/qq_43632766/article/details/90017859
注意需要下载
java ajax上传文件图片以json形式返回_第1张图片

package com.jdbc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.taglibs.standard.lang.jstl.test.beans.Factory;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class Ajaxtext
 */
@WebServlet("/Ajaxtext")
public class Ajaxtext extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Ajaxtext() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("/ajaxtext.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 request.setCharacterEncoding("utf-8");
		if(!ServletFileUpload.isMultipartContent(request)) {
			System.out.println("没有enctype属性");
			return ;
		}
		    DiskFileItemFactory factory = new DiskFileItemFactory();
		    factory.setDefaultCharset("utf-8");
			ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
			servletFileUpload.setHeaderEncoding("utf-8");
			try {
				List parseRequest = servletFileUpload.parseRequest(request);
				for(FileItem name:parseRequest) {
					if(name.isFormField()) {
					//普通表单项
					System.out.println("name"+name.getFieldName()+"value"+name.getString()+"---"+name.getName());
					}else {
					//文件项
						//这里进行上传图片的操作
						//不会可以参考 https://blog.csdn.net/qq_43632766/article/details/90017859
						System.out.println("name"+name.getFieldName());
		
					 }
					}
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/*
           *  这里只是教你如何返回
           */
	 response.setContentType("text/html;charset=utf-8");
	   PrintWriter writer = response.getWriter();
	   String[] code=new String[3];
	   code[0]="1";
	   code[1]="成功";
	   JSONObject json = new JSONObject();
	   json.put("code",code);
	   writer.print(json);
	}

}

你可能感兴趣的:(javaweb)