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 ...
随机推荐
- Java Mail解决标题乱码问题
在Java实现发送邮件功能时,直接使用 message.setSubject(subject) 的方式设置标题,在本地测试发送邮件的中文标题可以正常显示,但是将项目部署到服务器后,发送邮件的中文标题就 ...
- BZOJ 2150 部落战争 (二分图匹配)
题目大意:给你一个n*m的棋盘,有一些坏点不能走,你有很多军队,每支军队可以像象棋里的马一样移动,不过马是1*2移动的,而军队是r*c移动的,军队只能从上往下移动,如果一个点已经被一直军队经过,那么其 ...
- python和sudo python 出现no module named XXX
今天使用python执行文件,然而显示权限不够, 但是加上sudo之后,又会显示no module named XXX 问题在于两者python路径中包含的库不同.解决方法: 分别进入两个python ...
- 如何使用JAVA请求HTTPS
JDK对应的TLS版本(仅供参考) 1.写一个SSLClient类,继承至HttpClient import java.security.cert.CertificateException; impo ...
- 05006_Linux的jdk、mysql、tomcat安装
1.软件包下载链接:软件包下载 密码:advk 2.安装JDK (1)查看当前Linux系统是否已经安装java,输入 rpm -qa | grep java : (2)卸载两个openJDK (3) ...
- Mysql学习总结(35)——Mysql两千万数据优化及迁移
最近有一张2000W条记录的数据表需要优化和迁移.2000W数据对于MySQL来说很尴尬,因为合理的创建索引速度还是挺快的,再怎么优化速度也得不到多大提升.不过这些数据有大量的冗余字段和错误信息,极不 ...
- Maven学习总结(23)——Maven常用命令介绍
1.生成eclipse项目:mvn eclipse:eclipse 2.清除eclipse的一些系统设置:mvn eclipse:clean 3.mvn tomcat:run 在tomcat里面运行 ...
- @crossorigin注解跨域
在@controller中类的头部有一个@CrossOrigin注解. @CrossOrigin是用来处理跨域请求的注解 先来说一下什么是跨域: (站在巨人的肩膀上) 跨域,指的是浏览器不能执行其他网 ...
- Android开发之AudioManager(音频管理器)具体解释
AudioManager简单介绍: AudioManager类提供了訪问音量和振铃器mode控制. 使用Context.getSystemService(Context.AUDIO_SERVICE)来 ...
- hdu 2079 选课时间(题目已改动,注意读题) (母函数)
代码: #include<cstdio> #include<cstring> using namespace std; int main() { int t; scanf(&q ...