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. Mac 如何寻找Mac自带的IDLE

    Mac 如何寻找Mac自带的IDLE 每次要打开IDLE时,需要如下动作:打开terminal --> 输入idle --> 回车,就自动打开IDLE了 图标如下: 选择在“Finder中 ...

  2. 《图解HTTP》摘要

    网络基础TCP/IP 使用Cookie进行状态管理 HTTP首部 确保Web安全的HTTPS 1.网络基础TCP/IP 2.使用Cookie进行状态管理:HTTP是无状态协议. 3.HTTP首部 HT ...

  3. Redis数据库入门基础,及优缺点介绍

    简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis 是一个高性能的key-value数据库.R ...

  4. SQL中Group By的使用(转)

    1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”,然后针对若干个“小区域”进行数据处理. 2.原始表 3.简 ...

  5. 亲历:IT 从业者避免猝死攻略 v1.0

    作者:香蕉痞 出处:http://www.geekpark.net/read/view/191188?u=0 亲历:IT 从业者避免猝死攻略 v1.0 By 香蕉痞 | 2013/10/28 [核心提 ...

  6. libvirtd.service

    [root@kvm-server ~]# systemctl status libvirtd.service ● libvirtd.service - Virtualization daemon Lo ...

  7. css不定高度实现垂直居中

    1.不知道自己高度和父容器高度的情况下, 利用绝对定位只需要以下三行: parentElement{ position:relative; } childElement{ position: abso ...

  8. Linux下MySql数据库常用操作

    1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...

  9. angular-数据库

    <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng ...

  10. POJ——T 3159 Candies

    http://poj.org/problem?id=3159 Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 33328   ...