java 模拟ajax上传图片
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上传图片的更多相关文章
- java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱
//如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...
- Ajax上传图片以及上传之前先预览
手头上有几个小项目用到了easyUI,一开始决定使用easyUI就注定了项目整体上前后端分离,基本上所有的请求都采用Ajax来完成.在文件上传的时候用到了Ajax上传文件,以及图片在上传之前的预览效果 ...
- 模拟ajax实现网络爬虫——HtmlUnit
最近在用Jsoup抓取某网站数据,可有些页面是ajax请求动态生成的,去群里问了一下,大神说模拟ajax请求即可.去网上搜索了一下,发现了这篇文章,拿过来先用着试试. 转帖如下: 网上关 ...
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...
- 使用ajax上传图片,并且使用canvas实现出上传进度效果
前端代码: <%@ page contentType="text/html;charset=UTF-8" language="java" %> &l ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
- 利用gulp搭建本地服务器,并能模拟ajax
工作中可能会用到的小工具,在此记录一下.可以实现的功能有: 本地http服务器 页面实时刷新 可以模拟ajax请求 第一步,新建package.json文件.用到了gulp.gulp-webserve ...
- 模拟ajax的 script请求
/** * 模拟ajax的 script请求 * @param {[type]} options [description] * @return {[type]} [description] */ f ...
- 模拟ajax的同异步
今天突然想到那只在app中,如果请求数据时用的是app提供的接口,如果该接口没有同异步的话,怎么办. 所以就捣腾了下. 模拟ajax同异步. var VshopDataApi = { queryArr ...
随机推荐
- 优动漫PAINT安装教程
优动漫PAINT是一款漫画.插画.动画绘制软件.其功能可分别满足画师对于插画.漫画和动画创作的针对性需求,是一款非常好用易上手的动漫绘图软件,本文来看使用软件第一步,如何安装优动漫PAINT. 步骤一 ...
- Day 02 - 02 编程语言的分类
编程语言的分类 机器语言分为: 1.机器语言 优点:执行代码效率非常快 缺点:开发效率低 2.汇编语言 优点(相对于机器语言):开发效率高 缺点(相对于机器语言):执行效率低 3.高级语言 解释型(同 ...
- 05 ADO.net
ADO.NET就是一组类库 操作数据库用的.
- d3代码如何改造成update结构(恰当处理enter和exit)
d3的enter和exit 网上有很多blog讲解.说的还凑合的见:https://blog.csdn.net/nicolecc/article/details/50786661 如何把自己的rude ...
- POJ 3517 And Then There Was One( 约瑟夫环模板 )
链接:传送门 题意:典型约瑟夫环问题 约瑟夫环模板题:n个人( 编号 1-n )在一个圆上,先去掉第m个人,然后从m+1开始报1,报到k的人退出,剩下的人继续从1开始报数,求最后剩的人编号 /**** ...
- UVA10082-WERTYU(紫书例题3.2)
A common typing error is to place the hands on the keyboard one row to the right of the correct posi ...
- Memcached的实战笔记
官网:http://memcached.org/ 优秀Blogs: http://blog.csdn.net/jingqiang521/article/details/48345021 开启telne ...
- 小学生绞尽脑汁也学不会的python(反射)
小学生绞尽脑汁也学不会的python(反射) 1. issubclass, type, isinstance issubclass 判断xxxx类是否是xxxx类的子类 type 给出xxx的数据类型 ...
- POJ——T2352 Stars
http://poj.org/problem?id=2352 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46592 ...
- php学习之道:php empty()和isset()的差别
在使用 php 编写页面程序时,我常常使用变量处理函数推断 php 页面尾部參数的某个变量值是否为空,開始的时候我习惯了使用 empty() 函数,却发现了一些问题,因此改用 isset() 函数,问 ...