//App上传图片到App后端 App后端将图片传给PC项目的文件夹中
App将图片转为base64 以字符串形式传给App后端
//App后端接受
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String image = request.getParameter("img");
//得到存储的地址
uploadImg(files,image,imgName);
}
public void uploadImg(HashMap
//地址
Properties pps = new Properties();
pps.load(this.getClass().getClassLoader().getResourceAsStream("/img.properties"));
String s=pps.getProperty("url");
URL url = new URL(s+请求地址);
String param="image="+URLEncoder.encode(image,"UTF-8");
param+="&adress="+URLEncoder.encode(adress,"UTF-8");
HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
//设置参数
httpConn.setDoOutput(true); //需要输出
httpConn.setDoInput(true); //需要输入
httpConn.setUseCaches(false); //不允许缓存
httpConn.setRequestMethod("POST"); //设置POST方式连接
//设置请求属性
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 维持长连接
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Charset", "UTF-8");
httpConn.connect();
//输出流
DataOutputStream dos=new DataOutputStream(httpConn.getOutputStream());//PC接收
@RequestMapping("/receiveImg")
public String receive(HttpServletRequest request,HttpServletResponse response) throws Exception {
// 判断enctype属性是否为multipart/form-data
String image=request.getParameter("image");
String adress=request.getParameter("adress");
byte[] decode = Base64.getDecoder().decode(image);;
// 定义图片输入流
InputStream fin = new ByteArrayInputStream(decode);
String ppSendphoto=request.getSession().getServletContext().getRealPath(adress);
for(int i=0;i
if(decode[i]<0)
{//调整异常数据
decode[i]+=256;
}
}
OutputStream ou = new FileOutputStream(ppSendphoto);
ou.write(decode);
ou.flush();
fin.close();
ou.close();
response.setCharacterEncoding("UTF-8");
response.getWriter().println("上传成功");
return null;
}