8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

前言:

Android需求:用户上传图片,并且能够获取到用户上传的图片。

解决办法:用户选取图片时可以根据图片本机地址将图片转化为BitMap,然后转化为byte[]数组,利用Base64可以将数组转化为String,将String上传到服务器端,服务器根据String,将其你转化为图片格式,并以时间命名保存在服务器上,然后反馈给客户端图片在服务器上的URL

Android端:

android 图片 推送 android图片上传到服务器_android 图片 推送

1.选取图片,可以得到选取的图片的本地地址:

3private void (Intent data){
paths = ImageSelector.getImagePaths(data);

2.然后根据图片路径,将图片转化为String类型:1

*根据去的图片的路径生成BitMap然后转换为Byte[]并利用Base64将Byte[]加密编码转换成String*/
public List imageViewToString(){
for(int i=0;i
try {
FileInputStream fis = new FileInputStream(paths.get(i));
Bitmap bitmap = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();//将Bitmap转成Byte[]
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);//压缩,100表示不压缩
String imageString = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT);//加密转换成String
Log.e("WritePostActivity",imageString);
images.add(imageString);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return images;
/*for(int i=0;i
Log.e("WritePostActivity2",i+"-"+images.get(i));
}*/
}

3.创建工具类,用图片转换之后的string类型(就是这里的参数imgstr)来上传图片以及获得返回的图片服务器路径(就是这里的返回值s):

public class UploadImagesUtil{
public static final String url = String_Util.urlString+"GetImageAddressByImgstr";
public String upLoadPhoto(String imgStr){
if(imgStr==null){
System.out.println("imgStr is null");
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
BasicNameValuePair bnvp = new BasicNameValuePair("param", imgStr);
List list = new ArrayList();
list.add(bnvp);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity2 = response.getEntity();
String s = EntityUtils.toString(entity2);
return s;
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return null;
}
}

4.在活动中创建线程来调用工具类上传:

*上传图片String并得到服务器中图片地址的线程*/
private String photoUrls = "";//得到的服务器图片的地址,
private class UploadImagesThread extends Thread{
private List imgStrs;
public UploadImagesThread(List imgStrs){
this.imgStrs = imgStrs;
}
public void run(){
for(int i=0;i
/*调用工具类上传图片并得到返回的地址*/
String upLoadResults = new UploadImagesUtil().upLoadPhoto(imgStrs.get(i));
photoUrls = photoUrls+upLoadResults+",";//对地址进行拼接
}
Log.e("UploadImagesThread","结束");
}
}

这里的UploadResults为工具类(也就是服务器)返回的图片地址,将其通过逗号拼接成一个字符串photoUrls,photoUrls为该活动全局变量,

服务器端:

65public class GetImageAddressByImgstr extends HttpServlet{
private static final long serialVersionUID = 1L;
private String path;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
path = request.getRealPath("/") + "upload/";//服务器中图片文件夹
System.out.println(path);
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String param = request.getParameter("param");
String imgStr2Image = imgStr2Image(param);
out.print(imgStr2Image);
out.flush();
out.close();
}
//如果正确, 返回 url 不正确返回 error
private String imgStr2Image(String imgStr){
// TODO Auto-generated method stub
if(imgStr==null){
return "error:imgStr is null----MyImageServer";
}
File file = new File(path);
if (!file.exists()) {
System.out.println("no");
file.mkdir();
}else {
System.out.println("yes");
}
Date date = new Date();
long time = date.getTime();
String strName = time+".jpg";
String imgPath = path+strName;
String imgUrl = "http://192.168.1.4:8080/Mr_Right/upload/"+strName; //这里的192.168.1.4为以太网的ip地址,与安卓端不一样
try {
byte[] bs = new BASE64Decoder().decodeBuffer(imgStr);
for (int i = 0; i < bs.length; i++) {
if (bs[i] < 0) {
bs[i] += 256;
}
}
OutputStream out = new FileOutputStream(imgPath);
out.write(bs);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
return "error:IOException----MyImageServer";
}
return imgUrl;
}
}

创建服务器的图片文件夹:1path = request.getRealPath("/") + "upload/";//服务器中图片文件夹

将安卓端传递过来的string类型的图片转换成图片,并以时间命名存在图片文件夹下:1

15Date date = new Date();
long time = date.getTime();
String strName = time+".jpg";
String imgPath = path+strName;
byte[] bs = new BASE64Decoder().decodeBuffer(imgStr);
for (int i = 0; i < bs.length; i++) {
if (bs[i] < 0) {
bs[i] += 256;
}
}
OutputStream out = new FileOutputStream(imgPath);
out.write(bs);
out.flush();
out.close();

根据图片名直接就可以得到图片的地址:1String imgUrl = "http://192.168.1.4:8080/Mr_Right/upload/"+strName; //这里的192.168.1.4为以太网的ip地址,与安卓端不一样

然后安卓端的工具类会从该servlet请求到得到的地址。