1、maven 引入依赖

  

	<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>

  

 2、

@RequestMapping("upload")
public ResultMessage upload(HttpServletRequest request,HttpServletResponse response,
@RequestParam(name="userid",required=false)String userid,
@RequestParam(name="bid",required=false)String bid,
@RequestParam(name="pid",required=false)String pid,
@RequestParam(name="reportNumber",required=false)String reportNumber,
@RequestParam(name="plateNumber",required=false)String plateNumber,
@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
String filename=file.getOriginalFilename();
String extensionName = filename.substring(filename.lastIndexOf(".") + 1);
String imgPath = System.currentTimeMillis() + "." +extensionName;
String filePath = request.getSession().getServletContext().getRealPath("/")+"uploadimages\\";
File newfile = new File(filePath);
if(!newfile.exists()){
newfile.mkdirs();
}
file.transferTo(new File(filePath+imgPath));
HttpClient client = new DefaultHttpClient();
HttpPost post= new HttpPost("");
MultipartEntity muti = new MultipartEntity();
File newfile1 = new File(filePath+imgPath);
FileBody body = new FileBody(newfile1);
muti.addPart("file",body);
muti.addPart("userid", new StringBody(userid));
muti.addPart("pid", new StringBody(pid));
muti.addPart("bid", new StringBody(bid));
muti.addPart("reportNumber", new StringBody(reportNumber));
muti.addPart("plateNumber", new StringBody(plateNumber));
post.setEntity(muti);
try {
HttpResponse resp = client.execute(post);
if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
return ResultMessage.getSuccess().setData(EntityUtils.toString(resp.getEntity()));
}
}catch (IOException e) {
logger.info(e.getMessage(),e);
}
return ResultMessage.getFail(); }

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime --><dependency>    <groupId>org.apache.httpcomponents</groupId>    <artifactId>httpmime</artifactId>    <version>4.5.2</version></dependency>

java 模拟ajax上传图片的更多相关文章

  1. java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱

    //如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...

  2. Ajax上传图片以及上传之前先预览

    手头上有几个小项目用到了easyUI,一开始决定使用easyUI就注定了项目整体上前后端分离,基本上所有的请求都采用Ajax来完成.在文件上传的时候用到了Ajax上传文件,以及图片在上传之前的预览效果 ...

  3. 模拟ajax实现网络爬虫——HtmlUnit

        最近在用Jsoup抓取某网站数据,可有些页面是ajax请求动态生成的,去群里问了一下,大神说模拟ajax请求即可.去网上搜索了一下,发现了这篇文章,拿过来先用着试试.    转帖如下: 网上关 ...

  4. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  5. 使用ajax上传图片,并且使用canvas实现出上传进度效果

    前端代码: <%@ page contentType="text/html;charset=UTF-8" language="java" %> &l ...

  6. java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...

  7. 利用gulp搭建本地服务器,并能模拟ajax

    工作中可能会用到的小工具,在此记录一下.可以实现的功能有: 本地http服务器 页面实时刷新 可以模拟ajax请求 第一步,新建package.json文件.用到了gulp.gulp-webserve ...

  8. 模拟ajax的 script请求

    /** * 模拟ajax的 script请求 * @param {[type]} options [description] * @return {[type]} [description] */ f ...

  9. 模拟ajax的同异步

    今天突然想到那只在app中,如果请求数据时用的是app提供的接口,如果该接口没有同异步的话,怎么办. 所以就捣腾了下. 模拟ajax同异步. var VshopDataApi = { queryArr ...

随机推荐

  1. 备份和恢复ZBrush文件

    ZBrush可以自动保存绘图的备份副本,并在发生系统错误时提醒您恢复备份副本.当ZBrush软件遇到崩溃.导致错误.非正常退出的时候,可能之前所做的努力就会功亏一篑,那么,在ZBrush软件中能否将文 ...

  2. day13 基本的文件操作(好东西)

    目录 基本的文件处理 什么是文件 如何使用文件 使用Python写一个小程序控制文件 open(打开文件) read: readline:一次性读取一行 del:删除 close:关闭 write(写 ...

  3. BZOJ 3676 [Apio2014]回文串 (后缀自动机+manacher/回文自动机)

    题目大意: 给你一个字符串,求其中回文子串的长度*出现次数的最大值 明明是PAM裸题我干嘛要用SAM做 回文子串有一个神奇的性质,一个字符串本质不同的回文子串个数是$O(n)$级别的 用$manach ...

  4. Linux系统串口接收数据编

    http://blog.csdn.net/bg2bkk/article/details/8668576 之前基于IBM deveplopworks社区的代码,做了串口初始化和发送的程序,今天在此基础上 ...

  5. ucore_lab0

    一直想好好学习一下操作系统课程,去一个Mooc网站上找了一门操作系统的课程.这便是里面的配套实验. 实验指导:点这里 lab0主要是准备相关的操作环境.课程推荐使用qemu作为硬件模拟器,推荐运行环境 ...

  6. ASP.NET-Session与复杂数据类型

    原文链接:http://www.cnblogs.com/fish-li/archive/2013/05/28/3104750.html Session与复杂数据类型 Session有三种工作模式,拿A ...

  7. jquery让 readOnly失效的方法

    re.attr("readOnly","true"); re.attr("readOnly",false); 注意 :false不能带引号

  8. 编写html经常使用而又easy忘记的语句

    设置文件字符编码: <meta charset="utf-8"> 内部样式表: <style type="text/css"> hr { ...

  9. ida动态调试so,在init_array和JNI_ONLOAD处下断点

    本文涉及到的apk.请在github下载https://github.com/jltxgcy/AliCrack/AliCrackme_2.apk. 0x00 怎样在JNI_ONLOAD下断点.參考安卓 ...

  10. bzoj1218: [HNOI2003]激光炸弹(DP二维前缀和)

    1218: [HNOI2003]激光炸弹 题目:传送门 题解: 一道经典题目啊... 为了更好的操作...把整个坐标系向右上角移动,从(1,1)开始 那么f[i][j]统计一下以(i,j)作为右上角, ...