项目说明:
本代码只是一个简单的工具类,自己做的整合,采用sftp方式进行上传文件,如有错误,望指正!!!
功能:
1.读取本地指定文件夹的文件,进行扫描读取
2.将读取的文件上传至指定服务器的指定文件夹下
3.将上传的文件以二维码的方式输出到本地
3.1.二维码可以直接扫描下载或者观看
3.2.由于使用google的zxing设置utf-8后无效果,所以转换思维到将汉字生成汉语拼音的形式去生成二维码
3.3.生成二维码上方会显示生成的内容
实现代码:
服务器配置:
打开"/usr/local/tomcat/conf/server.xml"文件添加一下配置:
例如:
服务器位置是:http://ip:8080/usr/local/pipixia/1.png
配置之后直接访问:http://ip:8080/pipixia/1.png 即可
编辑
1.引入需求的maven
com.belerweb
pinyin4j
2.5.0
com.jcraft
jsch
0.1.54
commons-net
commons-net
3.6
com.alibaba
fastjson
1.2.47
com.google.zxing
core
3.3.0
com.google.zxing
javase
3.0.0
2.配置文件:ftp.properties,我这里用的是老方法,也可以使用@value注解获取yml文件的值
在resources中添加此文件
#服务器ip
ip = 你的服务器ip
#服务器登录用户名
user = 你的服务器登录用户
#登录密码
password = 你的服务器密码
#端口--默认22
port = 22
#服务器端储存文件的位置
filePath = /usr/local/
#本地需要上传文件位置
localPath = E:/Ruanjian/
#本地生成二维码保存地址
QRcodePath = E:/QRcode/
3.工具类:扫描指定文件目录下的文件
//扫描文件夹下面的文件
public static File[] getFile() throws IOException {
Properties properties = new Properties();
InputStream in = getQR.class.getResourceAsStream("/ftp.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
properties.load(bf);
String path = properties.getProperty("localPath");
File file = new File(path);
//list()方法是返回某个目录下的所有文件和目录的文件名,返回的是String数组
//listFiles()方法是返回某个目录下所有文件和目录的绝对路径,返回的是File数组
File[] files = file.listFiles();
return files;
}
4.工具类:将文件内容转换成byte[ ]类型
//将文件内容转成byte类型
public static byte[] File2byte(File tradeFile){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
5.工具类:上传文件至指定服务器,上传成功返回”ture“
//上传文件至服务器指定位置
public static String uploadFile(String fileName, String fileType) throws Exception {
Properties properties = new Properties();
InputStream in = getQR.class.getResourceAsStream("/ftp.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
properties.load(bf);
File file = new File(properties.getProperty("localPath")+fileName+"."+fileType);
byte[] bytes = File2byte(file);
String ip = properties.getProperty("ip");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
int port= Integer.parseInt(properties.getProperty("port"));
Session session = null;
Channel channel = null;
JSch jsch = new JSch();
if(port <=0){
//连接服务器,采用默认端口
session = jsch.getSession(user, ip);
}else{
//采用指定的端口连接服务器
session = jsch.getSession(user, ip ,port);
}
//如果服务器连接不上,则抛出异常
if (session == null) {
throw new Exception("session is null");
}
//设置登陆主机的密码
session.setPassword(password);//设置密码
//设置第一次登陆的时候提示,可选值:(ask | yes | no)
session.setConfig("StrictHostKeyChecking", "no");
//设置登陆超时时间
session.connect(30000);
OutputStream outstream = null;
try {
//创建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//进入服务器指定的文件夹
sftp.cd(properties.getProperty("filePath"));
//列出服务器指定的文件列表
// Vector v = sftp.ls("*");
// for(int i=0;i
6.工具类:生成二维码
//生成二维码--通用
public static void getSoft(String filename, String fileType) throws IOException, BadHanyuPinyinOutputFormatCombination {
Properties properties = new Properties();
InputStream in = getQR.class.getResourceAsStream("/ftp.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
properties.load(bf);
int width = 300; //定义图片宽度
int height = 300; //定义图片高度
String format = "jpg"; //定义图片格式
String pinyin = getQR.getPinyin(filename);
String content = "http://ip:8080/pipixia/"+pinyin+"."+fileType; //定义二维码内容
//定义二维码的参数
HashMap hints = new HashMap();
//白边的宽度,可取0~4
hints.put(EncodeHintType.MARGIN , 3);
//设置编码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//设置容错等级,等级越高,容量越小
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//生成二维码
try {
// 生成矩阵 内容 格式 宽 高
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//设置路径
Path file = new File(properties.getProperty("QRcodePath")+filename+".jpg").toPath();
//输出图像
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
log.info("
--------------------------------
"
+filename+".jpg二维码已生成
"+
"磁盘位置:"+properties.getProperty("QRcodePath")+filename+".jpg
"+
"--------------------------------
");
//将二维码加上文字
getQR.addTxt(filename, fileType);
} catch (Exception e) {
e.printStackTrace();
}
}
7.工具类:在生成二维码上方加入二维码信息
//在图片下方添加信息
public static void addTxt(String filename, String fileType) throws IOException {
Properties properties = new Properties();
InputStream in = getQR.class.getResourceAsStream("/ftp.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
properties.load(bf);
try {
//A.jpg是背景图
InputStream is = new FileInputStream(properties.getProperty("QRcodePath")+filename+".jpg");
//通过JPEG图象流创建JPEG数据流解码器
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
//解码当前JPEG数据流,返回BufferedImage对象
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
//得到画笔对象
Graphics g = buffImg.getGraphics();
//创建你要附加的图象。
//新的头像的路径
ImageIcon imgIcon = new ImageIcon(properties.getProperty("QRcodePath")+filename+"1.jpg");
//得到Image对象。
Image img = imgIcon.getImage();
//将小图片绘到大图片上。
//5,300 .表示你的小图片在大图片上的位置。
g.drawImage(img, 0, 0, null);
//设置颜色。
g.setColor(Color.BLACK);
//最后一个参数用来设置字体的大小
Font f = new Font("黑体", Font.PLAIN, 15);
Color mycolor = Color.BLACK;//new Color(0, 0, 255);
g.setColor(mycolor);
g.setFont(f);
//10,20 表示这段文字在图片上的位置(x,y) .第一个是设置的内容。
String text = filename+"."+fileType;
g.drawString(text, 10, 20);
g.dispose();
OutputStream os;
String shareFileName = properties.getProperty("QRcodePath")+filename+".jpg";
os = new FileOutputStream(shareFileName);
//创键编码器,用于编码内存中的图象数据。
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(buffImg);
is.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
8.主启动类:main方法中直接运行,也可以使用controller,此处只展示main方法运行效果
public static void main(String[] args) throws Exception {
File[] file = getQR.getFile();
for (File fs : file){
if (!fs.isDirectory()){
String name = fs.getName();
String fileType = name.substring(name.lastIndexOf(".")+1);
String fileName = name.substring(0,name.lastIndexOf("."));
String s = getQR.uploadFile(fileName, fileType);
if (s.equals("ture")){
getQR.getSoft(fileName,fileType);
log.info(fileName + "."+fileType+"上传成功!!!");
}else if (s.equals("false")){
log.info(fileName + "."+fileType+"上传失败!!请重试!!!");
}else {
log.info("发生未知错误,请检查代码!!!");
}
}else {
log.info("请添加文件以上传服务器!!!");
}
}
}
上传效果图:
执行显示:
编辑
服务器端:
编辑
生成的二维码:
编辑
方法到此处已完结!!!
如有错误,往大神指正!!
留言与评论(共有 0 条评论) “” |