上代码:
import com. alibaba. fastjson. JSON;
import com. yutu. fileupload. mapper. UserMapper ;
import com. yutu. fileupload. pojo. User ;
import com. yutu. fileupload. service. UserService ;
import org. apache. tools. tar. TarEntry ;
import org. apache. tools. tar. TarInputStream ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import java. io. * ;
import java. util. ArrayList ;
import java. util. List ;
import java. util. Map ;
import java. util. zip. GZIPInputStream ;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public List < User > findAllUser ( ) {
return userMapper. findAllUser ( ) ;
}
@Override
public boolean unTar ( ) {
String wjh33 = testUnTarGz ( ) ;
File file = new File ( wjh33) ;
List < Map > result = new ArrayList < > ( ) ;
File [ ] files = file. listFiles ( ) ;
for ( File f: files) {
if ( f. isDirectory ( ) ) {
File [ ] jsonFiles = f. listFiles ( ) ;
for ( File jf: jsonFiles) {
if ( ! jf. isDirectory ( ) ) {
File file1 = new File ( jf. getAbsolutePath ( ) ) ;
String jsonStr = readJsonFile ( jf) ;
Map jsonMap = ( Map ) JSON. parse ( jsonStr) ;
result. add ( jsonMap) ;
System . out. println ( "jsonStr: " + jsonStr) ;
}
}
}
}
if ( result != null && result. size ( ) > 0 ) {
for ( int i = 0 ; i< result. size ( ) ; i++ ) {
Map map = result. get ( i) ;
Map < String , String > param = result. get ( i) ;
String username = ( String ) map. get ( "username" ) ;
String password = ( String ) map. get ( "password" ) ;
String name = ( String ) map. get ( "name" ) ;
param. put ( "username" , username) ;
param. put ( "password" , password) ;
param. put ( "name" , name) ;
userMapper. insertUser ( param) ;
}
}
File file1 = new File ( wjh33) ;
file1. delete ( ) ;
return true ;
}
private String readJsonFile ( File jsonFile) {
String jsonStr = "" ;
System . out. println ( "————开始读取" + jsonFile. getPath ( ) + "文件————" ) ;
try {
FileReader fileReader = new FileReader ( jsonFile) ;
Reader reader = new InputStreamReader ( new FileInputStream ( jsonFile) , "utf-8" ) ;
int ch = 0 ;
StringBuffer sb = new StringBuffer ( ) ;
while ( ( ch = reader. read ( ) ) != - 1 ) {
sb. append ( ( char ) ch) ;
}
fileReader. close ( ) ;
reader. close ( ) ;
jsonStr = sb. toString ( ) ;
System . out. println ( "————读取" + jsonFile. getPath ( ) + "文件结束!————" ) ;
return jsonStr;
} catch ( Exception e) {
System . out. println ( "————读取" + jsonFile. getPath ( ) + "文件出现异常,读取失败!————" ) ;
e. printStackTrace ( ) ;
return null ;
}
}
private String testUnTarGz ( ) {
File srcTarGzFile = new File ( "f:/wjh.tar.gz" ) ;
String jieyePath = "f:/wjh33" ;
boolean boo = false ;
try {
unTarGz ( srcTarGzFile, jieyePath) ;
boo = true ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( ! boo)
deleteDirectory ( new File ( jieyePath) ) ;
}
return jieyePath;
}
private void unTarGz ( File file, String outputDir) throws IOException {
TarInputStream tarIn = null ;
try {
tarIn = new TarInputStream ( new GZIPInputStream (
new BufferedInputStream ( new FileInputStream ( file) ) ) ,
1024 * 2 ) ;
createDirectory ( outputDir, null ) ;
TarEntry entry = null ;
while ( ( entry = tarIn. getNextEntry ( ) ) != null ) {
if ( entry. isDirectory ( ) ) {
createDirectory ( outputDir, entry. getName ( ) ) ;
} else {
File tmpFile = new File ( outputDir + "/" + entry. getName ( ) ) ;
createDirectory ( tmpFile. getParent ( ) + "/" , null ) ;
OutputStream out = null ;
try {
out = new FileOutputStream ( tmpFile) ;
int length = 0 ;
byte [ ] b = new byte [ 2048 ] ;
while ( ( length = tarIn. read ( b) ) != - 1 ) {
out. write ( b, 0 , length) ;
}
} catch ( IOException ex) {
throw ex;
} finally {
if ( out!= null )
out. close ( ) ;
}
}
}
} catch ( IOException ex) {
throw new IOException ( "解压归档文件出现异常" , ex) ;
} finally {
try {
if ( tarIn != null ) {
tarIn. close ( ) ;
}
} catch ( IOException ex) {
throw new IOException ( "关闭tarFile出现异常" , ex) ;
}
}
}
private void createDirectory ( String outputDir, String subDir) {
File file = new File ( outputDir) ;
if ( ! ( subDir == null || subDir. trim ( ) . equals ( "" ) ) ) {
file = new File ( outputDir + "/" + subDir) ;
}
if ( ! file. exists ( ) ) {
file. mkdirs ( ) ;
}
}
private void deleteDirectory ( File file) {
if ( file. isFile ( ) ) {
file. delete ( ) ;
} else {
File list[ ] = file. listFiles ( ) ;
if ( list!= null ) {
for ( File f: list) {
deleteDirectory ( f) ;
}
file. delete ( ) ;
}
}
}
}
代码输出json文件中的数据:
2022 - 05 - 13 16 : 57 : 22.918 INFO 8064 -- - [ io- 18808 - exec- 2 ] o. a. c. c. C. [ Tomcat ] . [ localhost] . [ / ] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022 - 05 - 13 16 : 57 : 22.918 INFO 8064 -- - [ io- 18808 - exec- 2 ] o. s. web. servlet. DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022 - 05 - 13 16 : 57 : 22.919 INFO 8064 -- - [ io- 18808 - exec- 2 ] o. s. web. servlet. DispatcherServlet : Completed initialization in 1 ms
————开始读取f: \wjh33\wjh\a. json文件————
————读取f: \wjh33\wjh\a. json文件结束! ————
jsonStr: {
"username" : "a" ,
"password" : "apwd" ,
"name" : "a"
}
————开始读取f: \wjh33\wjh\b. json文件————
————读取f: \wjh33\wjh\b. json文件结束! ————
jsonStr: {
"username" : "b" ,
"password" : "bpwd" ,
"name" : "b"
}
————开始读取f: \wjh33\wjh\c. json文件————
————读取f: \wjh33\wjh\c. json文件结束! ————
jsonStr: {
"username" : "c" ,
"password" : "cpwd" ,
"name" : "c"
}
2022 - 05 - 13 16 : 57 : 23.142 INFO 8064 -- - [ io- 18808 - exec- 2 ] com. zaxxer. hikari. HikariDataSource : HikariPool - 1 - Starting . . .
2022 - 05 - 13 16 : 57 : 24.190 INFO 8064 -- - [ io- 18808 - exec- 2 ] com. zaxxer. hikari. HikariDataSource : HikariPool - 1 - Start completed.
入库: