/div>

No Description

PhotoUploadUtils.java 8.8KB

    package ai.pai.lensman.service; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import com.android.common.utils.LogHelper; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class PhotoUploadUtils { private URL url; private DataOutputStream ds; private HttpURLConnection conn; private Map<String, File> fileparams = new HashMap<String, File>(); private Map<String, String> textParams = new HashMap<String, String>(); private static final String boundary = "----WebKitFormBoundaryE19zNvXGzXaLvS5C"; public static final int UPLOAD_PHOTO_WIDTH = 1280; public static final int UPLOAD_PHOTO_HEIGHT = 1280; public PhotoUploadUtils(String url) throws Exception { this.url = new URL(url); } //增加一个普通字符串数据到form表单数据中 public void addTextParameter(String name, String value) { textParams.put(name, value); } //增加一个文件到form表单数据中 public void addFileParameter(String name, File value) { fileparams.put(name, value); } // 清空所有已添加的form表单数据 public void clearAllParameters() { textParams.clear(); fileparams.clear(); } // 发送数据到服务器,返回一个字节包含服务器的返回结果的数组 public byte[] send() throws Exception { initConnection(); conn.connect(); ds = new DataOutputStream(conn.getOutputStream()); writeFileParams(); writeStringParams(); paramsEnd(); InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); int b; while ((b = in.read()) != -1) { out.write(b); } conn.disconnect(); return out.toByteArray(); } public byte[] send(boolean isCompress) throws Exception { initConnection(); conn.connect(); ds = new DataOutputStream(conn.getOutputStream()); if(isCompress){ writeFileParams(); }else{ writeFileParamsWithoutCompress(); } writeStringParams(); paramsEnd(); InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(); int b; while ((b = in.read()) != -1) { out.write(b); } conn.disconnect(); return out.toByteArray(); } //文件上传的connection的一些必须设置 private void initConnection() throws Exception { conn = (HttpURLConnection) this.url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setConnectTimeout(10000); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); } //普通字符串数据 private void writeStringParams() throws Exception { Set<String> keySet = textParams.keySet(); for (Iterator<String> it = keySet.iterator(); it.hasNext();) { String name = it.next(); String value = textParams.get(name); ds.writeBytes("--" + boundary + "\r\n"); ds.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"\r\n"); ds.writeBytes("\r\n"); ds.writeBytes(encode(value) + "\r\n"); } } //文件数据 private void writeFileParams() throws Exception { Set<String> keySet = fileparams.keySet(); for (Iterator<String> it = keySet.iterator(); it.hasNext();) { String name = it.next(); File value = fileparams.get(name); ds.writeBytes("--" + boundary + "\r\n"); ds.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + encode(value.getName()) + "\"\r\n"); ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n"); ds.writeBytes("\r\n"); byte[] bmp = getBitmapBytes(value); ds.write(bmp); LogHelper.d("czy","upload final file length="+bmp.length); LogHelper.d("czy","Upload bitmap size:"+bmp.length); ds.writeBytes("\r\n"); // saveUploadFile(value,bmp); } } //文件数据 private void writeFileParamsWithoutCompress() throws Exception { Set<String> keySet = fileparams.keySet(); for (Iterator<String> it = keySet.iterator(); it.hasNext();) { String name = it.next(); File value = fileparams.get(name); ds.writeBytes("--" + boundary + "\r\n"); ds.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + encode(value.getName()) + "\"\r\n"); ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n"); ds.writeBytes("\r\n"); byte[] bmp = getBytes(value); ds.write(bmp); LogHelper.d("czy","upload final file length="+bmp.length); LogHelper.d("czy","Upload bitmap size:"+bmp.length); ds.writeBytes("\r\n"); // saveUploadFile(value,bmp); } } //获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream private String getContentType(File file) throws Exception { return "image/jpeg"; } private void saveUploadFile(File file,byte[] bmp){ String dirPath = Constants.APP_UPLOAD_DIR; try{ File uploadFile = new File(dirPath,file.getName()); FileOutputStream fos = new FileOutputStream(uploadFile); fos.write(bmp); fos.close(); LogHelper.d("czy","upload file save path = "+uploadFile.getAbsolutePath()); Bitmap finalBmp = BitmapFactory.decodeFile(uploadFile.getAbsolutePath()); LogHelper.dfile("czy","照片上传bitmap质量压缩后,宽 = "+finalBmp.getWidth()+"高="+finalBmp.getHeight()+"文件大小="+uploadFile.length()+"字节"); LogHelper.dfile("czy","\n----------------------------------------------------\n"); }catch (Exception e){ e.printStackTrace(); } } //把文件转换成字节数组 private byte[] getBitmapBytes(File f) throws Exception { int uploadWidth = UPLOAD_PHOTO_WIDTH; int uploadHeight = UPLOAD_PHOTO_HEIGHT; int uploadQuality = 90; LogHelper.d("czy","upload default width = "+uploadWidth+"uploadQuality="+uploadQuality); try{ File file = new File(Constants.APP_UPLOAD_CONFIG_PATH); if(file.exists()){ BufferedReader reader = new BufferedReader(new FileReader(file)); uploadHeight = Integer.parseInt(reader.readLine()); uploadWidth = uploadHeight; uploadQuality = Integer.parseInt(reader.readLine()); reader.close(); }else{ file.createNewFile(); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(String.valueOf(uploadHeight)); writer.write("\r\n"); writer.write(String.valueOf(uploadQuality)); writer.write("\r\n"); writer.close(); } }catch (Exception e){ e.printStackTrace(); } Bitmap bmp = ImageUtils.decodeBitmapWithSize(f.getAbsolutePath(), uploadWidth,uploadHeight); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.JPEG, uploadQuality, baos); return baos.toByteArray(); } //把文件转换成字节数组 private byte[] getBytes(File f) throws Exception { FileInputStream in = new FileInputStream(f); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = in.read(b)) != -1) { out.write(b, 0, n); } in.close(); return out.toByteArray(); } //添加结尾数据 private void paramsEnd() throws Exception { ds.writeBytes("--" + boundary + "--" + "\r\n"); ds.writeBytes("\r\n"); } // 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码 private String encode(String value) throws Exception { return URLEncoder.encode(value, "UTF-8"); } }
kodo - Gogs: Go Git Service

Brak opisu

basic_pc.html 6.2KB

    {% load staticfiles %} <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="format-detection" content="telephone=no,email=no,address=no"> <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"> <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" /> <title>{% block title %}{% endblock %}</title> <link href="//cdnjs.cloudflare.com/ajax/libs/pure/0.6.2/pure-min.css" rel="stylesheet"> <link href="//cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.3/flexslider.min.css" rel="stylesheet"> <link href="{% static 'page/css/flexslider.ext.css' %}" rel="stylesheet" type="text/css" /> {% block link %}{% endblock %} <style> img { pointer-events: none; } body { font-family: Microsoft YaHei,Helvetica Neue,Helvetica,Arial,sans-serif !important; } .container { padding-bottom: 45px; } .text-center { text-align: center; } .empty { color: #999; padding: .9375rem 0; font-size: .625rem; } .text_ellipsis { overflow:hidden; white-space:nowrap; text-overflow: ellipsis; } </style> {% block tohomecss %} <style> .tohome { width: 40px; height: 40px; background-color: rgba(139, 139, 139, 0.7); position: fixed; right: 10px; bottom: 20px; border-radius: 9999px; border-top-left-radius: 9999px; border-top-right-radius: 9999px; border-bottom-left-radius: 9999px; border-bottom-right-radius: 9999px; z-index: 99999999; } .tohome img { display: block; width: 18px; height: 13px; margin: 6px auto 2px; } .tohome p { color: #ffffff; } </style> {% endblock %} {% block css %}{% endblock %} <script> {% block rem %} //这段js的最后面有两个参数记得要设置,一个为设计稿实际宽度,一个为制作稿最大宽度,例如设计稿为750,最大宽度为750,则为(750,750) !function(e,t){function n(){var n=l.getBoundingClientRect().width;t=t||540,n>t&&(n=t);var i=100*n/e;r.innerHTML="html{font-size:"+i+"px;}"}var i,d=document,o=window,l=d.documentElement,r=document.createElement("style");if(l.firstElementChild)l.firstElementChild.appendChild(r);else{var a=d.createElement("div");a.appendChild(r),d.write(a.innerHTML),a=null}n(),o.addEventListener("resize",function(){clearTimeout(i),i=setTimeout(n,300)},!1),o.addEventListener("pageshow",function(e){e.persisted&&(clearTimeout(i),i=setTimeout(n,300))},!1),"complete"===d.readyState?d.body.style.fontSize="16px":d.addEventListener("DOMContentLoaded",function(e){d.body.style.fontSize="16px"},!1)}(1920,1920); {% endblock %} </script> </head> <body> {% block somehtml %}{% endblock %} <div class="container"> {% block container %}{% endblock %} {% block tohomehtml %} <div class="tohome"> <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACQAAAAeCAMAAABzP0xhAAAAS1BMVEUAAAD///////////////////////////////////////////////////////////////////////////////////////////////+DmQsHAAAAGHRSTlMAoHDw+q0VCufg187DuI+OgGJUSDwyKCBlxSbJAAAAi0lEQVQ4y+3SuRKEIBBFUaQbcd+X9/9fOpRDjaXQGJnNiW/SiwpkmXpiW6C16Sav4dR5qtlKHMpNbhaCR4vUTIwfnuKNxoWONR1uunB0g4Cxt9EbRDSXVewVoqr9bNYCgmJV3kwQ0fxtRkYCj8oZ8GBQqieHIWByev9DEPjfeisy+mDk6Dy9/kevRR9/MjHbEpIcQAAAAABJRU5ErkJggg=="> <p style="text-align:center;font-size:8px">回首页</p> </div> {% endblock %} </div> {% block somehtml2 %}{% endblock %} <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.3/jquery.flexslider-min.js"></script> {% block script %}{% endblock %} <script> {% block glbjs %}{% endblock %} $(function() { /** 格式化输入字符串**/ //用法: "hello{0}".format('world');返回'hello world' String.prototype.format= function(){ var args = arguments; return this.replace(/\{(\d+)\}/g,function(s,i){ return args[i]; }); } {% block flexsliderjs %} // 轮播图 $('.flexslider').flexslider({ slideshowSpeed: 2000, pauseOnHover: true, controlNav: false, directionNav: false, }); {% endblock %} {% block tohomejs %} // 返回首页 $('.tohome').click(function () { window.location.href = '{{ domain }}/page/nav?{{ params|safe }}' }) {% endblock %} {% block js %}{% endblock %} }); </script> <script type="text/javascript" src="//res.wx.qq.com/open/js/jweixin-1.2.0.js"></script> <script type="text/javascript" src="{% static 'machine/js/jswe-1.0.0.js' %}"></script> <script> {# V.initWxData({#} {# imgUrl: '{{ domain }}{% static 'page/img/logo.jpg' %}',#} {# link: 'http://hpsgift.hphcclub.com/we/ws',#} {# desc: '惠普销售红包系统',#} {# title: '惠普销售红包系统',#} {# timeLine: ''#} {# }, true);#} V.hideOptionMenu(); {% block jswe %}{% endblock %} </script> </body> </html>