springmvc 上传文件 上传多张照片 的APP接口
2017-08-09 19:38
387 查看
上传的方法
/**
* 图片上传
*/
public Map<String, String> ImageUpload(String user_id,String item_id,MultipartFile file,HttpServletRequest request,HttpServletResponse response)
throws IllegalStateException, IOException{
// String savePaths ="";
Map<String, String> map=new HashMap<>();
if (file!=null) {// 判断上传的文件是否为空
String path=null;// 文件路径
String type=null;// 文件类型
String fileName=file.getOriginalFilename();// 文件原名称
//System.out.println("上传的文件原名称:"+fileName);
// 判断文件类型
type=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
if (type!=null) {// 判断文件类型是否为空
if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
// 项目在容器中实际发布运行的根路径
String realPath="I:"+"/"+"user"+"/"+user_id+"/"+item_id;
// 自定义的文件名称
String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
// 设置存放图片文件的路径
path=realPath+/*System.getProperty("file.separator")+*/"/"+trueFileName;
File dir = new File(realPath,trueFileName);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
// // 转存文件到指定的路径
// file.transferTo(new File(realPath));
// savePaths =path;
map.put(trueFileName, path);
}else {
System.out.println("不是我们想要的文件类型,请按要求重新上传");
return null;
}
}else {
System.out.println("文件类型为空");
return null;
}
}else {
System.out.println("没有找到相对应的文件");
return null;
}
return map;
}
/**
* @Title:上传个人项目的方法
* @Description:
* @param file
* @param request
* @return
* @throws IOException
*/
public Map<String, String> uploadItemFile(String user_id,MultipartFile file) throws IOException{
//获得文件的上传路径I盘下的user文件夹+userID的文件夹+item_id的文件夹
String user="user";
//获得源文件的文件名
String fileName =file.getOriginalFilename();
//处理过得文件名
String fileNamegongcheng =System.currentTimeMillis()+fileName;
//处理源文件的名称
String filenamestr=fileName.substring(0,fileName.lastIndexOf("."));
//创建文件夹的路径
f45e
String path ="I:/"+user+"/"+user_id;
File dir = new File(path,fileNamegongcheng);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
//获得文件的路径
String realpath=path+"/"+fileNamegongcheng;
Map<String, String> map=new HashMap<>();
map.put(filenamestr, realpath);
//返回文件名 在写入数据库时使用
return map;
}
/**
* @Title:上传android/ios版本的方法
* @Description:
* @param file
* @param request
* @return
* @throws IOException
*/
public Map<String, String> uploadFile(String versiponType,MultipartFile file) throws IOException{
//获得文件的上传路径I盘下的versiponType文件夹
//获得源文件的文件名
String fileName =file.getOriginalFilename();
//处理过得文件名
String fileNamegongcheng =System.currentTimeMillis()+fileName;
//处理源文件的名称
String filenamestr=fileName.substring(0,fileName.lastIndexOf("."));
//创建文件夹的路径
String path ="I:/"+versiponType;
File dir = new File(path,fileNamegongcheng);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
//获得文件的路径
String realpath=path+"/"+fileNamegongcheng;
Map<String, String> map=new HashMap<>();
map.put(filenamestr, realpath);
//返回文件名 在写入数据库时使用
return map;
}
配置springmvc.xml
<!-- 上传拦截,如最大上传值及最小上传值 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>409600</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
接口方法
@RequestMapping(value="/uploaditem",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> insertItem(Item item,MultipartFile file) throws Exception{
Map<String, Object> map=new HashMap<>();
PageData pd=new PageData();
FileUpload upload=new FileUpload();
Map<String, String> itemmap=upload.uploadItemFile(item.getUSER_ID()+"", file);
if (itemmap!=null) {
//遍历map集合 首先建立set集合
Set<Map.Entry<String, String>> sm=itemmap.entrySet();
//再遍历集合
Iterator<Map.Entry<String, String>> it=sm.iterator();
//循环判断
while (it.hasNext()) {
//获得map的key:照片名 value:照片地址
Map.Entry<String, String> en=it.next();
String key=en.getKey();
String value=en.getValue();
//将key value 放入itemZp中
pd.put("USER_ID", item.getUSER_ID());
//判断是新建项目还是更新项目
if("新建".equals(item.getITEM_NAME())){
pd.put("ITEM_PARENT_ID", 0);
}else{
//判断更新的是哪个项目
PageData pd1=new PageData();
pd1.put("ITEM_NAME", value);
pd1.put("ITEM_PARENT_ID", 0);
int ITEM_PARENT_ID1=itemService.selectItemidByItemname(pd1);
pd.put("ITEM_PARENT_ID", ITEM_PARENT_ID1);
}
pd.put("ITEM_NAME", key);
pd.put("ITEM_URL", value);
pd.put("ITEM_SIZE", item.getITEM_SIZE());
pd.put("ITEM_TRADE1", item.getITEM_TRADE1());
pd.put("ITEM_TRADE2", item.getITEM_TRADE2());
pd.put("ITEM_TRADE3", item.getITEM_TRADE3());
pd.put("ITEM_SUBTITLE", item.getITEM_SUBTITLE());
pd.put("ITEM_DES", item.getITEM_DES());
pd.put("ITEM_UPDATELOG", item.getITEM_UPDATELOG());
pd.put("ITEM_DEVELOPER", item.getITEM_DEVELOPER());
pd.put("ITEM_VERSION", item.getITEM_VERSION());
}
if(itemService.insertItem(pd)>0){
map.put("status", 0);
map.put("msg", "上传项目成功!");
return map;
}else{
map.put("status", 1);
map.put("mag", "文件上传失败,请重新上传!");
return map;
}
}else{
map.put("status", 1);
map.put("mag", "请选择上传文件!");
return map;
}
}
/**
* @Title:批量上传照片
* @Description:
* @param user_id
* @param item_id
* @param request
* @param response
* @return
* @throws IOException
* @throws FileUploadException
*/
@RequestMapping(value = "/zpupload", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> zpUploadMethod(@RequestParam MultipartFile[] file,String user_id,String item_id,HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
Map<String, Object> returnMap=new HashMap<>();
//创建itemzp对象
ItemZp itemZp=null;
Upload upload=new Upload();
if(file!=null&&file.length>0){
for (int i = 0; i < file.length; i++) {
MultipartFile files = file[i];
Map<String, String> itemzpmap=upload.ImageUpload(user_id, item_id, files, request, response);
//遍历map集合 首先建立set集合
Set<Map.Entry<String, String>> sm=itemzpmap.entrySet();
//再遍历集合
Iterator<Map.Entry<String, String>> it=sm.iterator();
//循环判断
while (it.hasNext()) {
itemZp=new ItemZp();
itemZp.setItem_id(Integer.parseInt(item_id));
//获得map的key:照片名 value:照片地址
Map.Entry<String, String> en=it.next();
String key=en.getKey();
String value=en.getValue();
//将key value 放入itemZp中
itemZp.setZp_name(key);
itemZp.setZp_url(value);
//调用增加照片的方法
if(itemZpService.insertItemZp(itemZp)){
System.out.println(0);
}else{
System.out.println(1);
}
}
}
returnMap.put("Status", 0);
returnMap.put("msg", "上传照片成功!");
return returnMap;
}else {
returnMap.put("Status", 1);
returnMap.put("msg", "上传照片失败,请重新上传!");
return returnMap;
}
}
/**
* @Title:个人项目下载的方法
* @Description:
* @param user_id
* @param item_id
* @param request
* @param response
* @param storeName
* @param contentType
* @param realName
* @throws Exception
*/
@RequestMapping(value="/itemdown",method=RequestMethod.POST)
@ResponseBody
public void download(String item_id,HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType,
String realName) throws Exception {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//根据工程item_id查出user_id
Item item=itemService.selectItem(Integer.parseInt(item_id));
realName=item.getItem_name();
String downLoadPath =item.getItem_url();//下载的文件名
long fileLength = new File(downLoadPath).length();
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename="
+ new String(realName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
}
/**
* @Title:向APP展示项目图片的接口
* @Description:
* @param item_id
* @return map
*/
@RequestMapping(value="/showItemZp",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> showItemZp(@Validated String item_id){
Map<String, Object> map=new HashMap<>();
List<String> returnlistZpurl=new ArrayList<>();
List<ItemZp> listItemZp=itemZpService.selectItemZpByItemid(Integer.parseInt(item_id));
if (listItemZp!=null&&listItemZp.size()>0) {
for (ItemZp itemZp : listItemZp) {
returnlistZpurl.add(itemZp.getZp_url());
}
map.put("status", 0);
map.put("itemZp_url", returnlistZpurl);
map.put("msg", "成功!");
return map;
} else {
map.put("status", 1);
map.put("msg", "项目名不存在或此项目没有添加照片!");
return map;
}
}
/**
* @Title:向APP传送个人所有新建项目的接口
* @Description:
* @param user_id
* @return
*/
@RequestMapping(value="/showItemByUserid",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> showItemByUserid(String user_id){
Map<String, Object> map=new HashMap<>();
Item item=new Item();
item.setUser_id(Integer.parseInt(user_id));
item.setItem_parent_id(0);
//获得list的item
// Map<String,Object> item_nameMap=new HashMap<>();
List<Item> listItem=itemService.selectByUserid(item);
if (listItem!=null&&listItem.size()>0) {
// for (Item item2 : listItem) {
// item_nameMap.put("item_name", item2.getItem_name());
// }
map.put("status", 0);
map.put("result",listItem);
map.put("msg", "success");
return map;
} else {
map.put("status", 1);
map.put("msg", "fail");
return map;
}
}
/**
* 图片上传
*/
public Map<String, String> ImageUpload(String user_id,String item_id,MultipartFile file,HttpServletRequest request,HttpServletResponse response)
throws IllegalStateException, IOException{
// String savePaths ="";
Map<String, String> map=new HashMap<>();
if (file!=null) {// 判断上传的文件是否为空
String path=null;// 文件路径
String type=null;// 文件类型
String fileName=file.getOriginalFilename();// 文件原名称
//System.out.println("上传的文件原名称:"+fileName);
// 判断文件类型
type=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
if (type!=null) {// 判断文件类型是否为空
if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) {
// 项目在容器中实际发布运行的根路径
String realPath="I:"+"/"+"user"+"/"+user_id+"/"+item_id;
// 自定义的文件名称
String trueFileName=String.valueOf(System.currentTimeMillis())+fileName;
// 设置存放图片文件的路径
path=realPath+/*System.getProperty("file.separator")+*/"/"+trueFileName;
File dir = new File(realPath,trueFileName);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
// // 转存文件到指定的路径
// file.transferTo(new File(realPath));
// savePaths =path;
map.put(trueFileName, path);
}else {
System.out.println("不是我们想要的文件类型,请按要求重新上传");
return null;
}
}else {
System.out.println("文件类型为空");
return null;
}
}else {
System.out.println("没有找到相对应的文件");
return null;
}
return map;
}
/**
* @Title:上传个人项目的方法
* @Description:
* @param file
* @param request
* @return
* @throws IOException
*/
public Map<String, String> uploadItemFile(String user_id,MultipartFile file) throws IOException{
//获得文件的上传路径I盘下的user文件夹+userID的文件夹+item_id的文件夹
String user="user";
//获得源文件的文件名
String fileName =file.getOriginalFilename();
//处理过得文件名
String fileNamegongcheng =System.currentTimeMillis()+fileName;
//处理源文件的名称
String filenamestr=fileName.substring(0,fileName.lastIndexOf("."));
//创建文件夹的路径
f45e
String path ="I:/"+user+"/"+user_id;
File dir = new File(path,fileNamegongcheng);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
//获得文件的路径
String realpath=path+"/"+fileNamegongcheng;
Map<String, String> map=new HashMap<>();
map.put(filenamestr, realpath);
//返回文件名 在写入数据库时使用
return map;
}
/**
* @Title:上传android/ios版本的方法
* @Description:
* @param file
* @param request
* @return
* @throws IOException
*/
public Map<String, String> uploadFile(String versiponType,MultipartFile file) throws IOException{
//获得文件的上传路径I盘下的versiponType文件夹
//获得源文件的文件名
String fileName =file.getOriginalFilename();
//处理过得文件名
String fileNamegongcheng =System.currentTimeMillis()+fileName;
//处理源文件的名称
String filenamestr=fileName.substring(0,fileName.lastIndexOf("."));
//创建文件夹的路径
String path ="I:/"+versiponType;
File dir = new File(path,fileNamegongcheng);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
//获得文件的路径
String realpath=path+"/"+fileNamegongcheng;
Map<String, String> map=new HashMap<>();
map.put(filenamestr, realpath);
//返回文件名 在写入数据库时使用
return map;
}
配置springmvc.xml
<!-- 上传拦截,如最大上传值及最小上传值 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>409600</value>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
</bean>
接口方法
@RequestMapping(value="/uploaditem",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> insertItem(Item item,MultipartFile file) throws Exception{
Map<String, Object> map=new HashMap<>();
PageData pd=new PageData();
FileUpload upload=new FileUpload();
Map<String, String> itemmap=upload.uploadItemFile(item.getUSER_ID()+"", file);
if (itemmap!=null) {
//遍历map集合 首先建立set集合
Set<Map.Entry<String, String>> sm=itemmap.entrySet();
//再遍历集合
Iterator<Map.Entry<String, String>> it=sm.iterator();
//循环判断
while (it.hasNext()) {
//获得map的key:照片名 value:照片地址
Map.Entry<String, String> en=it.next();
String key=en.getKey();
String value=en.getValue();
//将key value 放入itemZp中
pd.put("USER_ID", item.getUSER_ID());
//判断是新建项目还是更新项目
if("新建".equals(item.getITEM_NAME())){
pd.put("ITEM_PARENT_ID", 0);
}else{
//判断更新的是哪个项目
PageData pd1=new PageData();
pd1.put("ITEM_NAME", value);
pd1.put("ITEM_PARENT_ID", 0);
int ITEM_PARENT_ID1=itemService.selectItemidByItemname(pd1);
pd.put("ITEM_PARENT_ID", ITEM_PARENT_ID1);
}
pd.put("ITEM_NAME", key);
pd.put("ITEM_URL", value);
pd.put("ITEM_SIZE", item.getITEM_SIZE());
pd.put("ITEM_TRADE1", item.getITEM_TRADE1());
pd.put("ITEM_TRADE2", item.getITEM_TRADE2());
pd.put("ITEM_TRADE3", item.getITEM_TRADE3());
pd.put("ITEM_SUBTITLE", item.getITEM_SUBTITLE());
pd.put("ITEM_DES", item.getITEM_DES());
pd.put("ITEM_UPDATELOG", item.getITEM_UPDATELOG());
pd.put("ITEM_DEVELOPER", item.getITEM_DEVELOPER());
pd.put("ITEM_VERSION", item.getITEM_VERSION());
}
if(itemService.insertItem(pd)>0){
map.put("status", 0);
map.put("msg", "上传项目成功!");
return map;
}else{
map.put("status", 1);
map.put("mag", "文件上传失败,请重新上传!");
return map;
}
}else{
map.put("status", 1);
map.put("mag", "请选择上传文件!");
return map;
}
}
/**
* @Title:批量上传照片
* @Description:
* @param user_id
* @param item_id
* @param request
* @param response
* @return
* @throws IOException
* @throws FileUploadException
*/
@RequestMapping(value = "/zpupload", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> zpUploadMethod(@RequestParam MultipartFile[] file,String user_id,String item_id,HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
Map<String, Object> returnMap=new HashMap<>();
//创建itemzp对象
ItemZp itemZp=null;
Upload upload=new Upload();
if(file!=null&&file.length>0){
for (int i = 0; i < file.length; i++) {
MultipartFile files = file[i];
Map<String, String> itemzpmap=upload.ImageUpload(user_id, item_id, files, request, response);
//遍历map集合 首先建立set集合
Set<Map.Entry<String, String>> sm=itemzpmap.entrySet();
//再遍历集合
Iterator<Map.Entry<String, String>> it=sm.iterator();
//循环判断
while (it.hasNext()) {
itemZp=new ItemZp();
itemZp.setItem_id(Integer.parseInt(item_id));
//获得map的key:照片名 value:照片地址
Map.Entry<String, String> en=it.next();
String key=en.getKey();
String value=en.getValue();
//将key value 放入itemZp中
itemZp.setZp_name(key);
itemZp.setZp_url(value);
//调用增加照片的方法
if(itemZpService.insertItemZp(itemZp)){
System.out.println(0);
}else{
System.out.println(1);
}
}
}
returnMap.put("Status", 0);
returnMap.put("msg", "上传照片成功!");
return returnMap;
}else {
returnMap.put("Status", 1);
returnMap.put("msg", "上传照片失败,请重新上传!");
return returnMap;
}
}
/**
* @Title:个人项目下载的方法
* @Description:
* @param user_id
* @param item_id
* @param request
* @param response
* @param storeName
* @param contentType
* @param realName
* @throws Exception
*/
@RequestMapping(value="/itemdown",method=RequestMethod.POST)
@ResponseBody
public void download(String item_id,HttpServletRequest request,
HttpServletResponse response, String storeName, String contentType,
String realName) throws Exception {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//根据工程item_id查出user_id
Item item=itemService.selectItem(Integer.parseInt(item_id));
realName=item.getItem_name();
String downLoadPath =item.getItem_url();//下载的文件名
long fileLength = new File(downLoadPath).length();
response.setContentType(contentType);
response.setHeader("Content-disposition", "attachment; filename="
+ new String(realName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(downLoadPath));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
}
/**
* @Title:向APP展示项目图片的接口
* @Description:
* @param item_id
* @return map
*/
@RequestMapping(value="/showItemZp",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> showItemZp(@Validated String item_id){
Map<String, Object> map=new HashMap<>();
List<String> returnlistZpurl=new ArrayList<>();
List<ItemZp> listItemZp=itemZpService.selectItemZpByItemid(Integer.parseInt(item_id));
if (listItemZp!=null&&listItemZp.size()>0) {
for (ItemZp itemZp : listItemZp) {
returnlistZpurl.add(itemZp.getZp_url());
}
map.put("status", 0);
map.put("itemZp_url", returnlistZpurl);
map.put("msg", "成功!");
return map;
} else {
map.put("status", 1);
map.put("msg", "项目名不存在或此项目没有添加照片!");
return map;
}
}
/**
* @Title:向APP传送个人所有新建项目的接口
* @Description:
* @param user_id
* @return
*/
@RequestMapping(value="/showItemByUserid",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> showItemByUserid(String user_id){
Map<String, Object> map=new HashMap<>();
Item item=new Item();
item.setUser_id(Integer.parseInt(user_id));
item.setItem_parent_id(0);
//获得list的item
// Map<String,Object> item_nameMap=new HashMap<>();
List<Item> listItem=itemService.selectByUserid(item);
if (listItem!=null&&listItem.size()>0) {
// for (Item item2 : listItem) {
// item_nameMap.put("item_name", item2.getItem_name());
// }
map.put("status", 0);
map.put("result",listItem);
map.put("msg", "success");
return map;
} else {
map.put("status", 1);
map.put("msg", "fail");
return map;
}
}
相关文章推荐
- springmvc图片文件上传接口
- SpringMVC 利用HttpPost向服务端接口上传文件
- Android客户端之“微服私访”App的系统学习(八)调用系统摄像头拍照并管理照片并使用Okhttp上传文件至后台
- 【收集】JAVA多文件 上传文件接口代码 -兼容app
- SpringMVC文件上传
- 非常牛叉的楼主,自己的问题其实就是答案--用springmvc上传文件时报The current request is not a multipart request异常
- 日志、Toast、网络、像素单位转换、屏幕、App相关、键盘、文件上传下载、加密、时间等工具类
- springmvc结合jquery.form.js异步提交表单上传文件
- SpringMVC学习记录(四)--文件上传与下载
- springMVC从上传的Excel文件中读取数据
- SpringMVC中的文件上传
- springMVC文件上传
- SpringMVC 学习笔记(八)文件上传
- Retrofit+RxJava实现app崩溃处理(一)上传文件
- SpringMVC核心类之处理注解URL核心类,文件上传核心类的用法和SpringMVC和service层的连接
- SpringMVC文件上传实现
- springmvc文件上传
- SpringMVC进行表单上的文件上传mutipartfile
- SpringMVC文件上传和下载
- Springmvc 利用apache.commons.fileupload上传文件