概述
小程序前端代码:
这里上传一张图片和两个数据
wx.uploadFile({
url: "http://localhost:8080/wechat/wechat",
filePath: "http://tmp/wxef9d9fa6091dbd8c.o6zAJsx_1Ky50578NSeL3g9RcnG4.bc1bdc4f54e601a1432762527e266d61.png",//图片路径,如tempFilePaths[0]
header: { "Content-Type": "multipart/form-data" },
name: 'image',
formData:{
a: encodeURI("123fdsfdsa12"),
b: encodeURI("12312"),
}
})
后端servlet接收:
这两个jar包是在一起使用的,我在建工程文件的时候因为版本的问题就出现了很多问题。
package wechat;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.json.JSONObject;
import org.apache.commons.*;
/**
* Servlet implementation class wechat
*/
public class wechat extends HttpServlet {
private static final long serialVersionUID = 1L;
private String uploadFilePath = "D:\upload";
File tempFile;
/**
* @see HttpServlet#HttpServlet()
*/
public wechat() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("图片存放");
request.setCharacterEncoding("UTF-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart)
{
String dir = "D:\upload";
File dirFile = new File(dir);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
try
{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
List<FileItem> items = fileUpload.parseRequest(request);
for (FileItem item : items) {
if (item.isFormField())
{
String name = item.getFieldName();
System.out.println(name+" "+URLDecoder.decode(item.getString(), "utf-8"));
}
else
{
System.out.println(dir);
System.out.println("item.getName=" + item.getName());
String longFileName = item.getName();
System.out.println("longFileName:" + longFileName);
char indexChar = '\';
String fileName = item.getName().substring(longFileName.lastIndexOf(indexChar) + 1, longFileName.length());
System.out.println("filePath=" + dir + "\" + fileName);
File saveFile = new File(dirFile, fileName);
item.write(saveFile);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
这就是简单的小程序图片上传实例。
因为很久以前写的了,所以把所有的都打包上传了,有需要的可以看一看:https://download.csdn.net/download/qq_38234785/12437989
最后
以上就是香蕉白猫为你收集整理的小程序图片,数据上传+servlet接收的全部内容,希望文章能够帮你解决小程序图片,数据上传+servlet接收所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复