博舍

使用二维码实现文件分享 怎么用ai制作二维码图片文件

使用二维码实现文件分享

有时候,想要把电脑上的文件快速便捷的传输到手机,就可以用到这个工具了。

使用方法示例:

  1、准备需要传输到手机的文件

   

  注:1.2版本不仅支持对文件的一键分享,还支持文件夹的一键分享。

  2、选中文件或文件夹,右键属性,点击QrShare

   

  3、弹出二维窗口,用手机扫描二维码下载文件

  

  提示:此时鼠标的粘贴板已经粘贴了二维码的内容,直接发给小伙伴,分析文件吧。

  4、手机下载文件结果

   

  下载时,服务端可以强制断开分享,也可以等待客户端下载完成后自动关闭分享;同时,客户端同时在线下载数量不能超过10个。

   当分享的文件类型是图片格式(png,jpg,gif,bmp,ico等)和网页格式(html,htm)时,浏览器会对其数据进行页面显示。即:直接显示图片而不是弹出下载图片(可通过另存为下载图片);直接显示html内容而不是提示下载文件。其他格式文件,直接提示下载文件。

 直接显示出图片。

注意:需要安装jdk1.6+,手机和电脑必须在同一局域网内,除非电脑部署在外网环境。

 

程序分享:

先说说使用的技术:

使用zxing生成二维码jdkswing的基础知识jdkhttpServerjdkzip压缩文件如何将可jar转换成可执行的exe文件修改windos注册表,实现右键菜单

代码如下:

QrShare.java

packagecom.kancy.httpserver;importjava.awt.Toolkit;importjava.awt.datatransfer.Clipboard;importjava.awt.datatransfer.StringSelection;importjava.awt.datatransfer.Transferable;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.net.InetAddress;importjava.net.InetSocketAddress;importjava.util.Hashtable;importjava.util.Map;importjava.util.Random;importjava.util.UUID;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjavax.swing.ImageIcon;importjavax.swing.JOptionPane;importcom.google.zxing.BarcodeFormat;importcom.google.zxing.EncodeHintType;importcom.google.zxing.MultiFormatWriter;importcom.google.zxing.WriterException;importcom.google.zxing.common.BitMatrix;importcom.sun.net.httpserver.HttpExchange;importcom.sun.net.httpserver.HttpHandler;importcom.sun.net.httpserver.HttpServer;/***使用二维码实现文件分享*@authorkancy*@see2018/07/30*/publicclassQrShare{publicstaticvoidmain(String[]arg){try{//1.获取文件路径,获取失败时,系统自动关闭Filefile=acquireAndCheckDownloadFile(arg);//2.启动服务startJavaHttpFileServer(file);//3.显示二维码Stringtitle="扫码下载文件("+file.getName()+")";Stringcontent="http://"+InetAddress.getLocalHost().getHostAddress().toString()+":"+HTTP_PORT+"/download"+getFileSuffix(file)+"?id="+System.currentTimeMillis();setSysClipboardText(content);createQrImageFrame(title,content);waitDelay(Setting.getProperty("wait_delay_time",Setting.wait_delay_time));//4.提示是否强制关闭服务intunAccomplishedSize=accomplishMap.size();if(unAccomplishedSize>0){intresult=JOptionPane.showConfirmDialog(null,"目前存在"+unAccomplishedSize+"个客户端正在下载,是否强制关闭服务?","系统关闭提示",2);if(result==0){closeService();System.exit(0);}}//5.系统退出waitAndExitSystem(0);}catch(Exceptione){alertAndExit("系统异常:"+e.getMessage());}}/***检查和获取文件*@paramarg*@return*/privatestaticFileacquireAndCheckDownloadFile(String[]arg){if(arg==null||arg.length5)){continue;}value+=num;if(value.length()==5){break;}}returnInteger.parseInt(value);}/***提示并且退出*@parammsg*/privatestaticvoidalertAndExit(Stringmsg){JOptionPane.showMessageDialog(null,msg);System.exit(0);}/***关闭服务*/privatestaticvoidcloseService(){if(!threadPool.isShutdown()){threadPool.shutdown();}server.stop(0);}/***关闭系统*@parami*@throwsInterruptedException*/privatestaticvoidwaitAndExitSystem(inti){intunAccomplishedSize=accomplishMap.size();if(unAccomplishedSize==0){closeService();System.out.println("系统即将关闭!");System.exit(0);}else{System.out.println("存在"+unAccomplishedSize+"个活动链接,请等待...");waitDelay(1000);waitAndExitSystem(i);}}/***睡眠*@paraml*/privatestaticvoidwaitDelay(longl){try{Thread.sleep(l);}catch(InterruptedExceptione){e.printStackTrace();}}/***将字符串复制到剪切板。*/privatestaticvoidsetSysClipboardText(StringwriteMe){Clipboardclip=Toolkit.getDefaultToolkit().getSystemClipboard();TransferabletText=newStringSelection(writeMe);clip.setContents(tText,null);}/***依赖com.google.zxingcore-3.0.0.jar**@paramtitle*@paramcontent*@throwsWriterException*/privatestaticvoidcreateQrImageFrame(Stringtitle,Stringcontent)throwsWriterException{Maphints=newHashtable();hints.put(EncodeHintType.CHARACTER_SET,Setting.default_charset);BitMatrixmatrix=newMultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,300,300,hints);intwidth=matrix.getWidth();intheight=matrix.getHeight();BufferedImageimage=newBufferedImage(width,height,1);for(intx=0;x0?Setting.http_port:getRandomPort();privatestaticHttpServerserver;privatestaticExecutorServicethreadPool=Executors.newFixedThreadPool(MAX_POOL_NUM);//只允许10个用户同时下载privatestaticMapaccomplishMap=newHashtable();//防止未下载完成,关闭服务/***开启http服务*@paramfile*@throwsException*/privatestaticvoidstartJavaHttpFileServer(Filefile)throwsException{server=HttpServer.create(newInetSocketAddress(HTTP_PORT),0);server.setExecutor(threadPool);server.createContext("/download"+getFileSuffix(file),newFileServerHandler(file));ThreadhttpServiceThread=newThread(newRunnable(){@Overridepublicvoidrun(){server.start();System.out.println("服务启动成功!");}});httpServiceThread.start();}/***获取文件后缀*@paramfile*@return*/privatestaticStringgetFileSuffix(Filefile){Stringname=file.getName();StringfileSuffix="";if(name.contains(".")){fileSuffix=name.substring(name.lastIndexOf("."),name.length());}returnfileSuffix.trim().toLowerCase();}/***文件下载静态处理器*@authorkancy*/staticclassFileServerHandlerimplementsHttpHandler{privateFilefile;publicFileServerHandler(Filefile){this.file=file;}@Overridepublicvoidhandle(HttpExchangeexchange)throwsIOException{if(accomplishMap.size()>=MAX_POOL_NUM){Stringmsg="服务队列已满,请稍候下载!";exchange.sendResponseHeaders(200,0);exchange.getResponseBody().write(msg.getBytes(Setting.default_charset));exchange.getResponseBody().flush();exchange.getResponseBody().close();return;}StringhandlerId=UUID.randomUUID().toString();try{//开始处理accomplishMap.put(handlerId,false);OutputStreamos=exchange.getResponseBody();FileInputStreamin=newFileInputStream(file);//判断文件的类型//1.图片类型/网页类型直接显示在浏览器,通过另存为下载//3.其他类型下载模式StringfileSuffix=getFileSuffix(file);StringcontentType=Constants.getContentType(fileSuffix);//设置下载头if(Constants.CONTENT_TYPE_DEFAULT.equals(contentType)||(!Setting.show_static_resource&&!Constants.CONTENT_TYPE_DEFAULT.equals(contentType))){exchange.getResponseHeaders().add("Content-Disposition","attachment;filename=""+newString(file.getName().getBytes(Setting.default_charset),Setting.content_disposition_charset)+""");}exchange.getResponseHeaders().add("Content-Type",contentType);exchange.getResponseHeaders().add("Content-Length",String.valueOf(file.length()));//发送响应exchange.sendResponseHeaders(200,file.length());//写文件writeAndClose(os,in);}finally{//处理完成accomplishMap.remove(handlerId);}}/***写文件并且关闭流*@paramos*@paramin*@throwsIOException*/privatestaticvoidwriteAndClose(OutputStreamos,FileInputStreamin)throwsIOException{try{byte[]buf=newbyte[1024];intsize=-1;while((size=in.read(buf))!=-1){os.write(buf,0,size);os.flush();}}finally{if(os!=null){os.close();}if(in!=null){in.close();}}}}}

ZipUtils.java

packagecom.kancy.httpserver;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.zip.ZipEntry;importjava.util.zip.ZipInputStream;importjava.util.zip.ZipOutputStream;/***ZIP工具**@authorkancy*@see2018/07/30*/publicclassZipUtils{publicstaticvoidunzip(StringsrcZip,StringoutPath){longstartTime=System.currentTimeMillis();try{ZipInputStreamZin=newZipInputStream(newFileInputStream(srcZip));//输入源zip路径BufferedInputStreamBin=newBufferedInputStream(Zin);FileFout=null;ZipEntryentry;try{while((entry=Zin.getNextEntry())!=null&&!entry.isDirectory()){Fout=newFile(outPath,entry.getName());if(!Fout.exists()){(newFile(Fout.getParent())).mkdirs();}FileOutputStreamout=newFileOutputStream(Fout);BufferedOutputStreamBout=newBufferedOutputStream(out);intb;while((b=Bin.read())!=-1){Bout.write(b);}Bout.close();out.close();System.out.println(Fout+"解压成功");}Bin.close();Zin.close();}catch(IOExceptione){e.printStackTrace();}}catch(FileNotFoundExceptione){e.printStackTrace();}longendTime=System.currentTimeMillis();System.out.println("耗费时间:"+(endTime-startTime)+"ms");}publicstaticvoidzip(StringzipFileName,FileinputFile)throwsException{System.out.println("压缩中...");Filefile=newFile(zipFileName);if(!file.exists())file.createNewFile();ZipOutputStreamout=newZipOutputStream(newFileOutputStream(file));BufferedOutputStreambo=newBufferedOutputStream(out);zip(out,inputFile,inputFile.getName(),bo);bo.close();out.close();//输出流关闭System.out.println("压缩完成");}publicstaticvoidzip(StringzipFileName,String...filePaths)throwsException{System.out.println("压缩中...");Filefile=newFile(zipFileName);if(!file.exists())file.createNewFile();ZipOutputStreamout=newZipOutputStream(newFileOutputStream(file));BufferedOutputStreambo=newBufferedOutputStream(out);for(inti=0;i

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。

上一篇

下一篇