⼩程序向Java传递信息及图⽚先看⼀下页⾯
记录⼀下遇到的问题
这⾥的经度纬度需要使⽤ Location来获取经纬度
success: function (res) {
var jingdu=res.latitude;
var weidu=res.longitude;
_District(res.latitude, res.longitude)
_this.setData({
jingdu:jingdu,
weidu:weidu
})
},
})
// 省
let province = sult.address_component.province;
// 市
let city = sult.address_component.city;
// 区
let district = sult.address_component.district;
let street = sult.address_component.street;
_page.setData({
district: sult.address_component.district,
region: [province, city, district, street,latitude,longitude],
sheng:province,
shi:city,
qu:district,
lu:street
})
}
})
},
这⾥key值也是我百度随便抄的⼀个(就不写出来了),可以去注册,获取⼀个
success (res){
const data = res.data;
var photodizhi=res.data;
that.setData({
photodizhi:photodizhi
})
//do something
}
})
}
})
},
拍摄这个按钮中有两个事件,⼀个是⽤于拍摄照⽚,⼀个是⽤于上传,将图⽚上传给我的Java后端,其中wx.uploadFile这个api中有⼀个url,是必填项,我的Java后端使⽤servlet接收,这是我这个servlet的地址
这⾥图⽚先传给servlet ,然后把图⽚传⾄指定的⽂件夹, 然后将地址传递回来,并且setData,将数据放在⾥⾯,⽅便下⾯使⽤
}
})
/**
// 数据只提交⼀次,点击提交后按钮为禁⽤状态
if (!this.data.isDisabled) {  //当disabled=false时
this.setData({
isDisabled: true,  //修改isDisabled的值为true(即启⽤状态)
text: "编辑"    //⽂字修改为“编辑”
})
}
else {    //当disabled=true时
this.setData({
isDisabled: false,    //修改isDisabled的值为false(即禁⽤状态)
text: "保存"  //⽂字修改为“保存”
})
}
*/
},
这是绑定提交按钮的事件,将个⼈信息, 及经纬度还有当前地址,以及图⽚地址⼀起传递给servlet
这⾥记录⼀下, 我原先的实现⽅法, 我原先是将图⽚上传放到了提交这个事件中,点击了提交后,图⽚与这些信息同时上传给servlet,但不是同⼀个servlet , 在上⾯的URL中即可看出来,⼀个传给testservlet,⼀个传给uploadServlet,然后我在uploadServlet 中将图⽚地址通过重定向传给第三个servlet,testServlet中的信息也是这样传递给第三个servlet,然后由第三个servlet传递给service(这⾥我们称第三个servlet为C), 在由service传递给dao层, 刚开始实现后没发现问题,在后续实现新功能的时候连续测试发现了⼀个问题, 通过重定向传递给C,数据会缓存在C中,⽽⼀旦触发提交事件后,由于C中有数据缓存,C会直接向service提交,⽽后其他两个servlet才刚获取到数据并且将数据发送给C,简易的说,就是那两个servlet执⾏的没C快,让它先跑了⼀步,没来的及将新数据传递给它,因此,我将上传图⽚直接放到拍摄按钮这个的事件中, 拍摄完成后, 直接将图⽚上传给servlet,⽽后将图⽚地址传递回来,将图⽚地址,当做⼀个普通数据传递跟其他的数据⼀起传递给testServlet,然后将数据存储就好了
这⾥上⼀下我两个servlet的代码
package servlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.URLDecoder;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apachemons.fileupload.FileItem;
import org.apachemons.fileupload.FileItemFactory;
import org.apachemons.fileupload.disk.DiskFileItemFactory;
import org.apachemons.fileupload.servlet.ServletFileUpload;
stService;
st;
@WebServlet("/test")
public class testServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
/* 设置响应头允许ajax跨域访问 */
response.setHeader("Access-Control-Allow-Origin", "*");
/* 星号表⽰所有的异域请求都可以接受, */
response.setHeader("Access-Control-Allow-Methods", "GET,POST");
String jingdu = Parameter("jingdu");
String weidu = Parameter("weidu");
// String Parameter("qw");
String dizhi = Parameter("dizhi");
String name = Parameter("name");
String wendu = Parameter("wendu");
String classname = Parameter("classname");
String photodizhi = Parameter("photodizhi");
System.out.println(name + classname + wendu+photodizhi);
System.out.println(jingdu);
System.out.println(weidu);
System.out.println(dizhi);
testService ts=new testService();
ts.add(name, classname, wendu, jingdu, weidu, dizhi, photodizhi);
// Properties capitals = new Properties();
// capitals.put(“name1”,name1);
// capitals.put(“wendu”,wendu);
/
/ capitals.put(“classname”,classname);
// capitals.put(“jingdu”,jingdu);
// capitals.put(“weidu”,weidu);
// capitals.put(“dizhi”,dizhi);
// OutputStream os = new FileOutputStream(“PropertiesUtil.properties”); // capitals.store(os, “a”);
// os.close();
}
@Override
protected void doPost(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 (!ists()) {
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 = FieldName();
System.out.println(name + " "
+ URLDecoder.String(), "utf-8"));
} else {
System.out.println(dir);
System.out.println("Name=" + Name());
String longFileName = Name();
System.out.println("longFileName:" + longFileName);
char indexChar = '\\';
String fileName = Name().substring(
longFileName.lastIndexOf(indexChar) + 1,
longFileName.length());
String photodizhi = dir + "\\" + fileName;
System.out.println("filePath=" + dir + "\\" + fileName);
File saveFile = new File(dirFile, fileName);
item.write(saveFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
*/
}
}
package servlet;
>有趣的java小程序

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。