java后台接受android端图片上传的代码怎么写?

碧海银剑 2017-01-06 09:25:45
给这个要求整懵了!
@POST
@Path("insertSelective")
@Produces("application/json")
@Consumes(MediaType.APPLICATION_JSON) //指定接受的数据类型
@Transactional
public int insertSelective(AccountInfo record) {
Map<String, Object> resultMap = new HashMap<String, Object>();
// TODO Auto-generated method stub
int resultcode=accountInfoService.insertSelective(record);
resultMap.put("code", resultcode);
switch (resultcode) {
case 1:
resultMap.put("msg", "插入用户信息成功");
break;
case 0:
resultMap.put("msg", "插入用户信息失败");
break;
default:
break;
}
return resultcode;
}
这是后台的接口风格,求大神点拨!
...全文
985 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_17204061 2017-01-07
  • 打赏
  • 举报
回复
//获取网站的真实路径并加上上传目录 String root = ServletActionContext.getServletContext().getRealPath("/"); String dir = "upload"+File.separator + (now.getYear()+1900) + File.separator + (now.getMonth()+1); File dirFile = new File(root+dir); //如果上传目录不存在,则新建 if(!dirFile.exists()){ dirFile.mkdirs(); } String houzui = ""; if(imageFileName.indexOf(".")>-1){ //取出后缀名,如.jpg houzui = imageFileName.substring(imageFileName.lastIndexOf(".")); } //文件的相对路径 String filePath = dir + File.separator + UUID.randomUUID()+houzui; //将上传文件作为输入流 FileInputStream fis = new FileInputStream(image); //将要保存的文件作为输出流 File saveFile = new File(root + filePath); FileOutputStream fos = new FileOutputStream(saveFile); int len = 0; byte[] buf = new byte[512]; while( (len = fis.read(buf)) > 0 ){ fos.write(buf, 0, len); } fis.close(); fos.close();
鸣鸣Amadues 2017-01-07
  • 打赏
  • 举报
回复
apache有个fileupload的插件,去下一个就可以了。 以前我做过,前端是安卓的,可以用。
碧海银剑 2017-01-06
  • 打赏
  • 举报
回复
引用 4 楼 qq_17280849 的回复:
@Override
	public JSONObject saveNewInfo2(CBDNewModel n,HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		JSONObject json=new JSONObject();
		JSONObject retjson=new JSONObject();
		json.put("status", "ERROR");
		json.put("message", "保存失败");
		retjson.put("rest_code", "ERROR");
		retjson.put("message", "保存失败");
		if(StrUtil.isNull(n.getTitle())){
			json.put("message", "请输入商圈标题");
		}else if(StrUtil.isNull(n.getContent())){
			json.put("message", "请输入商圈内容");
		}
		
		/*else if(StrUtil.isNull(n.getTypefatherid())){
			json.put("message", "请输入商圈父类型id");
		}else if(StrUtil.isNull(n.getTypefathername())){
			json.put("message", "请输入商圈父类型名称");
		}else if(StrUtil.isNull(n.getTypesonid())){
			json.put("message", "请输入商圈子类型id");
		}else if(StrUtil.isNull(n.getTypesonname())){
			json.put("message", "请输入商圈子类型名称");
		}*/
		
		else if(StrUtil.isNull(n.getUserid())){
			json.put("message", "请传入发布人");
		}else{
			//CBDNewModel n=new CBDNewModel();
			n.setId(new UNIDGenerate().toString());
			n.setCreatetime(DateUtils.getCurrTime());
			n.setIsshow("Y");
			n.setIsrecommend("N");
			n.setIstop("N");
			n.setDisable("N");
			n.setViews(1);
			try {
				n.setTitle(URLDecoder.decode(n.getTitle(),"utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				n.setContent(URLDecoder.decode(n.getContent(),"utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			//String path = request.getServletContext().getRealPath("/")+FinalUtil.NEW_INFO_IMAGES ; 
			//String path = FinalUtil.NEW_INFO_IMAGES ; 
			//创建一个通用的多部分解析器  
	        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
	        //判断 request 是否有文件上传,即多部分请求  
	        if(multipartResolver.isMultipart(request)){  
	        	String imaUrl = newUploadImage(request, n.getUserid());
	        	if(!StrUtil.isNull(imaUrl)){
	        		n.setImageurl(imaUrl);
	        		Integer count = newMapper.insertSelective(n);
	        		if(count>0){
	        			retjson.put("rest_code", "SUCCESS");
		        		retjson.put("message", "发布商机成功,请等待审核!");
		        		json.put("status", "SUCCESS");
		        		json.put("message", "发布商机成功,请等待审核!");
	        		}
	        	}else{
					retjson.put("message", "请上传图片");
	        	}
	        }else{
	        	retjson.put("rest_code", "ERROR");
				retjson.put("message", "请上传图片");
	        }
		}
		json.put("data", retjson);
		return json;
	}
比如这个,开个接口saveNewInfo2(Model n),里面接受android端传过来的对象model,然后, 你把这个request里面的img上传到服务器【重新生成路径,取request里面的文件名】,然后把上传之后返回的图片地址写进你对应存储对象的字段里面。 这样别人一调用你这个接口,就会在服务器生成图片,并且对应路径存在数据库
大神,对,我觉得你这个写法很成熟,你看看我那个接口,如果这个接口要接收一张图片改怎么改!因为我用的是spring mvp mybatis架构,对于你代码的那种写法,我还是比较陌生的!ps,我是写前端的,后台没人!
雨上小公举 2017-01-06
  • 打赏
  • 举报
回复
@Override
	public JSONObject saveNewInfo2(CBDNewModel n,HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		JSONObject json=new JSONObject();
		JSONObject retjson=new JSONObject();
		json.put("status", "ERROR");
		json.put("message", "保存失败");
		retjson.put("rest_code", "ERROR");
		retjson.put("message", "保存失败");
		if(StrUtil.isNull(n.getTitle())){
			json.put("message", "请输入商圈标题");
		}else if(StrUtil.isNull(n.getContent())){
			json.put("message", "请输入商圈内容");
		}
		
		/*else if(StrUtil.isNull(n.getTypefatherid())){
			json.put("message", "请输入商圈父类型id");
		}else if(StrUtil.isNull(n.getTypefathername())){
			json.put("message", "请输入商圈父类型名称");
		}else if(StrUtil.isNull(n.getTypesonid())){
			json.put("message", "请输入商圈子类型id");
		}else if(StrUtil.isNull(n.getTypesonname())){
			json.put("message", "请输入商圈子类型名称");
		}*/
		
		else if(StrUtil.isNull(n.getUserid())){
			json.put("message", "请传入发布人");
		}else{
			//CBDNewModel n=new CBDNewModel();
			n.setId(new UNIDGenerate().toString());
			n.setCreatetime(DateUtils.getCurrTime());
			n.setIsshow("Y");
			n.setIsrecommend("N");
			n.setIstop("N");
			n.setDisable("N");
			n.setViews(1);
			try {
				n.setTitle(URLDecoder.decode(n.getTitle(),"utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			try {
				n.setContent(URLDecoder.decode(n.getContent(),"utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			//String path = request.getServletContext().getRealPath("/")+FinalUtil.NEW_INFO_IMAGES ; 
			//String path = FinalUtil.NEW_INFO_IMAGES ; 
			//创建一个通用的多部分解析器  
	        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
	        //判断 request 是否有文件上传,即多部分请求  
	        if(multipartResolver.isMultipart(request)){  
	        	String imaUrl = newUploadImage(request, n.getUserid());
	        	if(!StrUtil.isNull(imaUrl)){
	        		n.setImageurl(imaUrl);
	        		Integer count = newMapper.insertSelective(n);
	        		if(count>0){
	        			retjson.put("rest_code", "SUCCESS");
		        		retjson.put("message", "发布商机成功,请等待审核!");
		        		json.put("status", "SUCCESS");
		        		json.put("message", "发布商机成功,请等待审核!");
	        		}
	        	}else{
					retjson.put("message", "请上传图片");
	        	}
	        }else{
	        	retjson.put("rest_code", "ERROR");
				retjson.put("message", "请上传图片");
	        }
		}
		json.put("data", retjson);
		return json;
	}
比如这个,开个接口saveNewInfo2(Model n),里面接受android端传过来的对象model,然后, 你把这个request里面的img上传到服务器【重新生成路径,取request里面的文件名】,然后把上传之后返回的图片地址写进你对应存储对象的字段里面。 这样别人一调用你这个接口,就会在服务器生成图片,并且对应路径存在数据库
碧海银剑 2017-01-06
  • 打赏
  • 举报
回复
引用 2 楼 qq_17280849 的回复:
你开个接口,上传图片,图片保存在服务器,路径保存在i数据库
大神,我可以看出你的思路很清晰,但是三大框架我一个都不会,大神能不能稍微详细一点!
雨上小公举 2017-01-06
  • 打赏
  • 举报
回复
你开个接口,上传图片,图片保存在服务器,路径保存在i数据库
碧海银剑 2017-01-06
  • 打赏
  • 举报
回复
来个大神带我飞!
  • 打赏
  • 举报
回复
引用 3 楼 碧海银剑的回复:
[quote=引用 2 楼 qq_17280849 的回复:] 你开个接口,上传图片,图片保存在服务器,路径保存在i数据库
大神,我可以看出你的思路很清晰,但是三大框架我一个都不会,大神能不能稍微详细一点![/quote] 三大框架不会,用servlet,二进制获取

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧