-
java存数据到mysql_java实现将远程数据ftp上传到本地,并存入到mysql数据库里
2021-01-27 01:43:471、Configpackage ...import java.text.SimpleDateFormat;import java.util.Date;public class Config {public static final String ftpPath="/PIftp/Yzps/ps001/ps";//public static final String ft...1、Config
package com.lianlife.sync;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Config {
public static final String ftpPath="/PIftp/Yzps/ps001/ps";
//public static final String ftpPath="/PIftp/test01/ps";
public static final String c1 = "17:30:00";
public static final String c2 = "17:29:00";
public static final String ftpIP = "10.20.28.138";
public static final int ftpPort = 21;
public static final String ftpuser = "ftpuser";
public static final String ftpassword = "ftpuser";
//public static String localDirPath="H:\\yx1\\yq\\ps20150309";
public static String localDirPath="I:\\影像2\\ps2015030923";
public static String date = "20040901";
public static String date2 = "20150926";
public static final String url = "jdbc:mysql://10.20.28.138:3306/mysql";
public static final String driver = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "123";
public static boolean isUploadTimePeriod()
{
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
String timenow = df.format(new Date());
if (timenow.compareTo(Config.c1) > 0
|| timenow.compareTo(Config.c2) < 0){
return true;
}
return false ;
}
}
2、DbUtil
package com.lianlife.sync;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DbUtil {
public static Connection getConnection()
{
try {
Class.forName(Config.driver);//指定连接类型
} catch (Throwable e) {
throw new RuntimeException(e);
}
try {
return DriverManager.getConnection(Config.url, Config.user, Config.password);//获取连接
} catch (Throwable e) {
e.printStackTrace();
try {
return DriverManager.getConnection(Config.url, Config.user, Config.password);//获取连接
} catch (Throwable e2) {
e2.printStackTrace();
try {
return DriverManager.getConnection(Config.url, Config.user, Config.password);//获取连接
} catch (Throwable e3) {
throw new RuntimeException(e3);
}
}
}
}
public static void close(Connection c)
{
if(c!=null)
{
try {
c.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement c)
{
if(c!=null)
{
try {
c.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
public static void close(ResultSet c)
{
if(c!=null)
{
try {
c.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
3、
原文:http://liu16.blog.51cto.com/6494747/1720718
-
java将文件存在本地和取本地文件
2019-08-20 16:58:391 存文件到本地指定路径 public static void mywrite(String str){ File file = new File(str); //1、建立连接 OutputStream os = null; try { //2、选择输出流,以追加形式(在原有内容上追加) 写出文件 必须为...1 存文件到本地指定路径
public static void mywrite(String str){
File file = new File(str); //1、建立连接
OutputStream os = null;
try {
//2、选择输出流,以追加形式(在原有内容上追加) 写出文件 必须为true 否则为覆盖
os = new FileOutputStream(file,true);
// //和上一句功能一样,BufferedInputStream是增强流,加上之后能提高输出效率,建议
// os = new BufferedOutputStream(new FileOutputStream(file,true));
String string = “Programmer say : Hello World!”;
byte[] data = string.getBytes(); //将字符串转换为字节数组,方便下面写入os.write(data, 0, data.length); //3、写入文件 os.flush(); //将存储在管道中的数据强制刷新出去 } catch (FileNotFoundException e) { e.printStackTrace(); System.out.println("文件没有找到!"); } catch (IOException e) { e.printStackTrace(); System.out.println("写入文件失败!"); }finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("关闭输出流失败!"); } } } }
public static void main(String[] args) {
mywrite(“F:/Tool/nginx/nginx-1.12.2/html/hello/1.txt”);}
本地成功的创建了1.txt文件
2 取到本地的文件内容
File file = new File(“F:/Tool/nginx/nginx-1.12.2/html/hello/1.txt”);
StringBuilder localStrBulider = new StringBuilder();
if (file.isFile() && file.exists()) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), “utf-8”);
BufferedReader bufferReader = new BufferedReader(inputStreamReader);
String lineStr = null;
try {
while ((lineStr = bufferReader.readLine()) != null) {
localStrBulider.append(lineStr);
}
bufferReader.close();
inputStreamReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(“file read error!”);
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(“file catch unsupported encoding!”);
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println(“file not found!”);
e.printStackTrace();
}
} else {
System.out.println(“file is not a file or file is not existing!”);
return null;
}
System.out.println(“localStrBulider.toString():” + localStrBulider.toString());
读取成功! -
java实现图片临时储存在本地,之后统一上传到图片服务器
2021-03-23 15:58:22调用第一个接口时,先将多张图片存到本地;再调用第二个接口,将图片统一上传到图片服务器上。 二、应用场景 根据类别,上传多张图片 三、代码实现 1、controller层 @ApiOperation("临时上传图片") @...一、概述
调用第一个接口时,先将多张图片存到本地;再调用第二个接口,将图片统一上传到图片服务器上。
二、应用场景
根据类别,上传多张图片
三、代码实现
1、controller层
@ApiOperation("临时上传图片") @PostMapping("/img/temp") public JsonResult uploadFileBufferToLocal(@NotNull @RequestParam @ApiParam("图片所属种类)") String photoType, @NotNull @Image @ApiParam("不超过10M,请上传jpg和png文件") MultipartFile file) { return service.uploadFileBufferToLocal(photoType, file); } @ApiOperation("上传照片") @PostMapping("/upload") public JsonResult upload(@NotNull @RequestParam @ApiParam("图片类别") Integer type) { return service.upload(type); }
2、serviceImpl层
缓存到本地的功能实现
public JsonResult uploadFileBufferToLocal(String photoType, MultipartFile file) { //将文件缓存到本地 MultipartFile localFile = createLocalFile(photoType, file); if (localFile == null) { log.error("Create local file failed!"); return JsonResult.faild("Create local file failed!"); } log.info("Create local file successfully"); return JsonResult.success("Create local file successfully"); } /** * 通过上传的文件名,缓冲到本地,后面才能解压、验证 * * @param photoType 图片类型 * @param file */ public MultipartFile createLocalFile(String photoType, MultipartFile file) { String filePath = "C:/upload/temp/"; File localFile = new File(filePath); //先创建目录 localFile.mkdirs(); String originalFilename = file.getOriginalFilename(); if (originalFilename.contains(",")) { originalFilename = originalFilename.replace(",", ""); } if (originalFilename.contains(" ")) { originalFilename = originalFilename.replace(" ", ""); } String newname = photoType + "_" + originalFilename; String path = filePath + "/" + newname; log.info("createLocalFile path = {}", path); localFile = new File(path); FileOutputStream fos = null; InputStream in = null; try { if (localFile.exists()) { //如果文件存在删除文件 boolean delete = localFile.delete(); if (delete == false) { log.error("Delete exist file \"{}\" failed!!!", path, new Exception("Delete exist file \"" + path + "\" failed!!!")); } } //创建文件 if (!localFile.exists()) { //如果文件不存在,则创建新的文件 localFile.createNewFile(); log.info("Create file successfully,the file is {}", path); } //创建文件成功后,写入内容到文件里 fos = new FileOutputStream(localFile); in = file.getInputStream(); byte[] bytes = new byte[1024]; int len = -1; while ((len = in.read(bytes)) != -1) { fos.write(bytes, 0, len); } FileInputStream fileInputStream = new FileInputStream(localFile); MultipartFile multipartFile = new MockMultipartFile(localFile.getName(), localFile.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); fos.flush(); log.info("Reading uploaded file and buffering to local successfully!"); return multipartFile; } catch (FileNotFoundException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } finally { try { if (fos != null) { fos.close(); } if (in != null) { in.close(); } } catch (IOException e) { log.error("InputStream or OutputStream close error : {}", e); return null; } } }
上传到服务器的功能实现
public JsonResult upload(Integer type) { String filepath = "C:/upload/temp/"; File localFile = new File(filepath);//File类型可以是文件也可以是文件夹 File[] files = localFile.listFiles();//将该目录下的所有文件放置在一个File类型的数组中 QueryWrapper<Entity> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("type", type); Entity entity = entityDao.selectOne(queryWrapper); String result = ""; try { for (int i = 0; i < files.length; i++) { InputStream inputStream = new FileInputStream(files[i]); MultipartFile multipartFile = new MockMultipartFile(files[i].getName(), inputStream); /*处理传入文件名中的空格及逗号*/ String originalFilename = multipartFile.getName(); InputStreamResource isr = new InputStreamResource(multipartFile.getInputStream(), originalFilename); Map<String, Object> params = new HashMap<>(); params.put("file", isr); params.put("path", idWorkerUtil.nextId()); params.put("output", "json"); params.put("auth_token", fastdfsAuthCode); //上传图片到gofastdfs服务器 result = HttpUtil.post(uploadUrl, params); //删除本地文件 files[i].delete(); entityDao.updateById(entity); } } catch (IOException e) { log.error("错误为=" + e.getMessage(), e); } return JsonResult.success(); }
四、感谢
本文中的本地文件储存来源博主的这篇文章,https://blog.csdn.net/weixin_30462049/article/details/97326852,欢迎大家移驾交流学习。
-
java write不清空文件_FileWriter的write()方法,我传一个String进去,怎么有时存不了...
2021-03-17 15:00:05/** 本类的用处是将url指定的网页打开并保存在本地的html文件* 在生成本类的实例时构造方法传入url* 在调用huoqu()能在服务器获取到html的源代码(不包含接收头)* 通过baocun()将huoqu()中返回的字符串保存成本地的...该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package jiao.down;
/*
* 本类的用处是将url指定的网页打开并保存在本地的html文件
* 在生成本类的实例时构造方法传入url
* 在调用huoqu()能在服务器获取到html的源代码(不包含接收头)
* 通过baocun()将huoqu()中返回的字符串保存成本地的html文件
* */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownHtml {
String urlString;
static String name = "lalala";
//
public DownHtml(String urlString,String name) {
this.urlString = urlString;
this.name = name;
}
public String huoqu() throws Exception {
//生成一个URL对象
URL url = new URL(urlString);
//打开URL
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//得到输入流,即获得了网页的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
.getInputStream()));
String sum = "";
String line;
// 读取输入流的数据,并显示
while ((line = reader.readLine()) != null){
// System.out.println(line);
sum = sum +line+"\n";
}
// System.out.println(sum);
return sum;
}
public static void baocun(String date) throws IOException
{
FileWriter fw = new FileWriter(name+".html");
System.out.println("date:*****************************");
System.out.println(date);
System.out.println("date:*****************************");
fw.write(date);
System.out.println("下载成功!!");
}
}
-
JAVA上百实例源码以及开源项目
2016-01-03 17:37:40Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥... -
Android本地存储数据方法(超简单)
2017-03-28 13:48:12需求:将字符串保存到本地文件,可随时取出使用,可支持Map<String,String>。 解决方法:用SharedPreferences做存储。 实现: 提取出两个方法,一个存,一个取。直接放在项目的工具类CommonUtil.java中即可... -
将类中的成员函数写出来后,报错:本地函数非法
2013-06-17 05:50:17//指针对象,最多存50个学生数据 ofstream *file[50]; //负责对文件插入操作 int i=0; int j=0; //int flag=0;//flag判断输入是否有效 //do { //cin>>p; //if((p>='0'&&p)) //flag=1; //else // ... -
JAVA上百实例源码以及开源项目源代码
2018-12-11 17:07:42Java生成密钥的实例 1个目标文件 摘要:Java源码,算法相关,密钥 Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、... -
封装给java使用的 web插件报表 (修正版本)
2009-06-22 17:01:36这是个封装给java使用的 web插件报表,这个市场上已经存在,但是是个商业报表: ...修正:增加支持多数据源,报表显示完成内存化,本地不存任何的数据 新建QQ群:29672297 有疑问请发邮件到 lujianfu123@yahoo.com.cn -
《Java开发实战1200例(第I卷)》(李钟尉.陈丹丹).part1 高清完整PDF版
2016-06-13 15:46:48书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉.陈丹丹) 2011年1月出版 PDF格式扫描版,共压缩为5部分,这是第1部分。 内容简介 本系列图书包括第I卷、第II卷共计1200个例子,本文档是第I卷,共计603... -
《Java开发实战1200例(第I卷)》(李钟尉.陈丹丹).part4 高清完整PDF版
2016-06-13 16:13:13书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉.陈丹丹) 2011年1月出版 PDF格式扫描版,共压缩为5部分,这是第4部分。 内容简介 本系列图书包括第I卷、第II卷共计1200个例子,本文档是第I卷,共计603... -
《Java开发实战1200例(第I卷)》(李钟尉.陈丹丹).part2 高清完整PDF版
2016-06-13 15:53:27书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉.陈丹丹) 2011年1月出版 PDF格式扫描版,共压缩为5部分,这是第2部分。 内容简介 本系列图书包括第I卷、第II卷共计1200个例子,本文档是第I卷,共计603... -
《Java开发实战1200例(第I卷)》(李钟尉.陈丹丹).part3 高清完整PDF版
2016-06-13 16:11:24书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉.陈丹丹) 2011年1月出版 PDF格式扫描版,共压缩为5部分,这是第3部分。 内容简介 本系列图书包括第I卷、第II卷共计1200个例子,本文档是第I卷,共计603... -
《Java开发实战1200例(第I卷)》(李钟尉.陈丹丹).part5 高清完整PDF版
2016-06-13 16:17:38书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉.陈丹丹) 2011年1月出版 PDF格式扫描版,共压缩为5部分,这是第5部分。 内容简介 本系列图书包括第I卷、第II卷共计1200个例子,本文档是第I卷,共计603... -
Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part3
2016-06-12 11:39:31书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉,陈丹丹) PDF格式扫描版,全书分为24章,共817页。2011年1月出版。 全书压缩打包成4部分,这是第3部分 注:本系列图书的第I、II卷再版时均相应改名为... -
Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part1
2016-06-12 11:34:39书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉,陈丹丹) PDF格式扫描版,全书分为24章,共817页。2011年1月出版。 全书压缩打包成4部分,这是第1部分 注:本系列图书的第I、II卷再版时均相应改名为... -
Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part2
2016-06-12 11:37:04书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉,陈丹丹) PDF格式扫描版,全书分为24章,共817页。2011年1月出版。 全书压缩打包成4部分,这是第2部分 注:本系列图书的第I、II卷再版时均相应改名为... -
Java开发实战1200例(第1卷).(清华出版.李钟尉.陈丹丹).part4
2016-06-12 11:41:40书名:《Java开发实战1200例(第I卷)》(清华大学出版社.李钟尉,陈丹丹) PDF格式扫描版,全书分为24章,共817页。2011年1月出版。 全书压缩打包成4部分,这是第4部分 注:本系列图书的第I、II卷再版时均相应改名为... -
上传三张图片数据库里用三个不同字段存,结果出现了三条数据(逐步更新的)是怎么回事啊
2021-02-02 18:21:01//将图片保存到static文件夹里 fileUpload.transferTo(new File(filePath+fileName)); String filename="/static/images/"+fileName; drivecertification=filename; fileUpload1.... -
Android录音播放工具类,仿微信语音聊天。
2015-11-02 20:08:37代码是从聊天类项目中抽出来的,播放类中含有本地播放和网络播放,做法仿微信,先将网络路径中的.amr文件下载下来,然后存到本地播放。 一、录音类import java.io.File; import java.io.IOException;import android... -
Redis
2020-10-19 22:12:46redis的value有类型且有操作方法,所以可以将redis的数据类型作为集合操作,将java的操作移动到redis,实现java无状态。 Redis是单线程的,5.0之后worker线程时单线程的;但是io线程可以多线程的 五大数据类型 1、... -
springMVC poi解析ajax上传excel文件,返回json对象\list数组
2018-06-09 16:36:43一行中的一列,就把当前列头为key,列值为value存到该列的Map中 Map 一个线性Hash Map,以Excel的sheet表顺序,并以sheet表明作为key,sheet表转换Json后的字符串作为value 最后返回一个LinkedHashMap<String, ... -
finishToActivity : 结束到指定 Activity finishOtherActivities : 结束所有其他类型的 Activity finishAllActivities : 结束所有 Activity finishAllActivitiesExceptNewest: 结束除最新之外的所有 Activity ...
-
怎么做这道题 有些代码已经有了怎么改?
2019-10-25 10:38:522.在Eclipse中新建一个项目(自行命名),将实验一中的第5小题的库存商品管理类移植到JSP项目中,实现通过web界面进行库存商品的管理操作,商品库存信息是通过本地文件的读写实现的。 要求:编写6个JSP页面,分别... -
工程硕士学位论文 基于Android+HTML5的移动Web项目高效开发探究
2017-02-28 21:22:19Chrome Frame 会把最新版的Chrome Webkit 内核和JavaScript 引擎注入到IE中, IE浏览器将获得Chrome的性能和功能 目录 摘要 I ABSTRACT II 专业名词清单 III 第一章 绪论 1 1.1 研究背景与意义 1 1.2国内外相关... -
自动登录的原理就是,用户第一登录时会有一个token为null,如果登录成功,那么就在本地存一个token,关掉应用(不是退出登录)再次打开的时候,判断token是否正确,如果正确就自动登录。 这里本地存放token使用了...
-
C#微软培训教材(高清PDF)
2009-07-30 08:51:17C#--微软.NET的第一语言 ... 在本章中你将了解 Microsoft.NET 的概念 .NET 框架 C#语言在.NET 框架中的作用及其特性 1.1 Microsoft.NET 一场新的革命 1.1.1 什么是.NET 2000 年 6 月 ... -
C#微软培训资料
2014-01-22 14:10:17<<page 1>> page begin==================== 目 目目 目 录 录录 ... 2000 年 6 月 22 日 不论对 Microsoft 还是对整个 IT 业界都将成为值得纪念的一天 这一天 微软公司正式推出了其下一代...