Ajax+formData异步文件上传

2021-06-28 18:04

阅读:570

YPE >

标签:lin   exce   text   ace   附件   get   request   error   .json   

前端jsp:

"java" import="java.util.*" pageEncoding="UTF-8"%>

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

  

    "">

    

    

<span>ajax</span><span>文件上传</span>

  

  function add(){

   var formdata=new FormData($("#frm")[0]);

   $.ajax({

   type:‘post‘,

   url:‘ajaxupload.do‘,

   data:formdata,

   dataType:‘json‘,

   contentType:false,

   processData:false,

   success:function(data){

   var result=data.result;

   if(result==‘1‘){

   alert(‘提交成功!‘);

   }else{

   alert(‘提交失败!‘);

   }

   },

   error:function(){

   alert(‘未知错误!‘);

   }

   });

   }

  

  

  

  

  

"post" id="frm">

  

  

  

  

  

  

  

标题: "text" name="title" />
内容: "text" name="content" />
附件一: "file" name="ff" />
附件二: "file" name="ff2" />
"2">"button" onclick="add()" value="提交">
 

  

  

 

 

后端servlet java代码:

package servlet;

 

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.List;

import java.util.UUID;

 

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.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.json.JSONObject;

 

public class AjaxUploadServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

 

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

DiskFileItemFactory factory=new DiskFileItemFactory();

ServletFileUpload upload=new ServletFileUpload(factory);

JSONObject js=new JSONObject();

String title="";

String content="";

try {

List items=upload.parseRequest(request);

for(FileItem item:items){

if(item.isFormField()){

//只能通过此形式获取普通表单域的值,上传文件,表单form的enctype="multipart/form-data",是以二进制数据流提交数据的。所以没法用request.getParameter("name")来获取提交到后台的普通表单域值。

if("title".equals(item.getFieldName())){

title=item.getString("UTF-8");

}else if("content".equals(item.getFieldName())){

content=item.getString("UTF-8");

}

}else{

String fileName=item.getName();

System.out.println("上传的文件为:"+fileName);

String path=request.getRealPath("/upload");

File f=new File(path+"/"+UUID.randomUUID()+"-"+fileName);

item.write(f);

js.put("result", "1");

}

}

System.out.println("title:"+title+",content:"+content);

} catch (Exception e) {

e.printStackTrace();

}

out.write(js.toString());

}

 

}

 

Ajax+formData异步文件上传

标签:lin   exce   text   ace   附件   get   request   error   .json   

原文地址:http://www.cnblogs.com/sherry11/p/7142627.html


评论


亲,登录后才可以留言!