微信小程序上传图片+java 后端接收实例

在网上看了好多小程序上传图片,java后端接收的示例,但是不管在哪个网站看的,代码基本是一样的,都是代码特别多。

所以就自己写一个比较简单的。

 一 小程序端

user.wxml

<view class='user_head'>
  <view>
    <image src='{{ptuser.avatarUrl}}' bindtap='updateHead'></image>
  </view>
  <text>点击选择头像</text>
</view>

user.js

 // 更换头像
  updateHead: function () {
    var that = this
    // 上传图片 获取路径
    wx.chooseImage({
      success: function (res) {
        console.log('临时路径:' + res.tempFilePaths[0])
            wx.uploadFile({
              url: app.globalData.baseUrl + '/file/uploadFile',
              filePath: res.tempFilePaths[0],
              name: 'file',
              success: function (result) {
                console.log("返回路径:" + result.data)
              }
            })
      },
    })
  },

 二 java端

package cn.helloxhs.moudle.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.helloxhs.base.controller.BaseController;

/**
 * 类说明
 * 
 * @author 肖荷山
 * @version 创建时间:2017年12月23日 上午11:14:27
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController {
	@RequestMapping("/uploadFile")
	@ResponseBody
	public Object uploadFile(HttpServletResponse response, HttpServletRequest request, MultipartFile file) {
		String realPath = request.getSession().getServletContext().getRealPath("/temp");
		try {
			CommonsMultipartFile cf = (CommonsMultipartFile) file;
			DiskFileItem fi = (DiskFileItem) cf.getFileItem();
			File f1 = fi.getStoreLocation();
			InputStream ips = new FileInputStream(f1);
			OutputStream ops = new FileOutputStream(realPath + "/" + "xhs.jpg");
			byte[] b = new byte[1024];
			int len;
			try {
				while ((len = ips.read(b)) != -1) {
					ops.write(b, 0, len);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				// 完毕,关闭所有链接
				try {
					ops.close();
					ips.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return realPath;
	}

}
图片存在了项目的temp目录下


简单就好,没其他功能,单纯上传图片





  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
微信小程序开发是一种基于微信平台的应用开发,可以使用多种语言进行后端开发,包括JavaJava是一种稳定、可靠、跨平台的编程语言,非常适合用于开发后端管理系统。 下面是一个简要的微信小程序开发java后端管理教程: 1. 确定需求:首先要明确管理系统的功能需求,例如用户管理、权限管理、数据展示等。 2. 架构设计:设计合理的系统架构,确定数据库的表结构,以及后端接口的设计。 3. 创建项目:使用Java开发工具,如Eclipse或IntelliJ IDEA,创建一个Java项目。 4. 引入依赖:通过Maven或Gradle等构建工具,引入相关依赖库,例如Spring Boot、MyBatis等。 5. 编写实体类:根据需求设计数据库表结构,创建相应的实体类。 6. 编写DAO层:使用MyBatis等持久层框架,编写与数据库交互的DAO层代码。 7. 编写Service层:编写处理业务逻辑的Service层代码,并调用相应的DAO层代码。 8. 设计接口:使用Spring MVC等Web框架,设计并编写后端接口,用于与前端小程序进行数据交互。 9. 实现接口:根据接口设计,在Controller层编写接口的实现代码,包括数据的查询、增、删、改等操作。 10. 部署项目:将项目打包成可执行的jar文件,使用服务器软件(如Tomcat)进行部署。 11. 调试测试:进行接口的调试和测试,确保各项功能正常。 12. 上线发布:将完成的后端管理系统发布到线上服务器,供用户使用。 以上是一个简要的微信小程序开发java后端管理教程,具体的开发步骤还需要根据实际需求和项目情况进行调整和完善。同时,还需了解微信小程序开发的相关文档和API,以便实现与微信平台的交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的蜗牛905

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值