Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
T
traffic-front
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangxiaoming
traffic-front
Commits
e84c5a33
Commit
e84c5a33
authored
Oct 25, 2018
by
eddie.woo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modify upload
parent
5f275d23
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
17 deletions
+35
-17
TemplateController.java
.../java/pwc/taxtech/atms/controller/TemplateController.java
+9
-6
HttpFileService.java
...n/java/pwc/taxtech/atms/service/impl/HttpFileService.java
+24
-9
TemplateGroupServiceImpl.java
...c/taxtech/atms/service/impl/TemplateGroupServiceImpl.java
+2
-2
No files found.
atms-api/src/main/java/pwc/taxtech/atms/controller/TemplateController.java
View file @
e84c5a33
package
pwc
.
taxtech
.
atms
.
controller
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.joda.time.DateTime
;
import
org.slf4j.Logger
;
...
...
@@ -91,14 +92,16 @@ public class TemplateController extends BaseController {
String
customFileName
=
"template_"
+
DateTime
.
now
().
toString
(
"yyyyMMddHHmmss"
)
+
".xlsx"
;
response
.
setHeader
(
"Content-Disposition"
,
String
.
format
(
"inline; filename=\""
+
customFileName
+
"\""
));
response
.
setContentType
(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
int
len
=
0
;
byte
[]
buffer
=
new
byte
[
1024
];
// int len = 0;
// byte[] buffer = new byte[1024];
// out = response.getOutputStream();
// while ((len = inputStream.read(buffer)) > 0) {
// out.write(buffer, 0, len);
// }
out
=
response
.
getOutputStream
();
while
((
len
=
inputStream
.
read
(
buffer
))
>
0
)
{
out
.
write
(
buffer
,
0
,
len
);
}
IOUtils
.
copy
(
inputStream
,
out
);
out
.
flush
();
//FileCopyUtils.copy(inputStream, response.getOutputStream());
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
...
...
atms-api/src/main/java/pwc/taxtech/atms/service/impl/HttpFileService.java
View file @
e84c5a33
package
pwc
.
taxtech
.
atms
.
service
.
impl
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.commons.io.Charsets
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang3.CharSet
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.http.Consts
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpHeaders
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.BasicHttpEntity
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.entity.mime.MultipartEntityBuilder
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
pwc.taxtech.atms.common.config.FileServiceConfig
;
import
pwc.taxtech.atms.dto.ApiResultDto
;
import
pwc.taxtech.atms.exception.ServiceException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.nio.charset.StandardCharsets
;
import
java.util.HashMap
;
import
java.util.Map
;
@Service
public
class
HttpFileService
extends
BaseService
{
@Autowired
private
FileServiceConfig
config
;
private
static
final
String
USER_TEMPLATE_PATH
=
"pwc/userTemplate/"
;
private
static
final
String
HTTP_HEADER
=
"application/json;charset=UTF-8"
;
/**
* 上传模板
*
* @param fileName
文件名
* @param
inputStream inputStream
* @param fileName 文件名
* @param
file MultipartFile
* @return Boolean
*/
public
String
uploadTemplate
(
String
fileName
,
InputStream
inputStream
)
throws
ServiceException
{
if
(
StringUtils
.
isBlank
(
fileName
)
||
null
==
inputStream
)
{
public
String
uploadTemplate
(
String
fileName
,
MultipartFile
file
)
throws
ServiceException
{
if
(
StringUtils
.
isBlank
(
fileName
)
||
null
==
file
)
{
throw
new
IllegalArgumentException
(
"上传参数为空"
);
}
CloseableHttpClient
httpClient
=
null
;
...
...
@@ -42,11 +55,13 @@ public class HttpFileService extends BaseService {
try
{
httpClient
=
HttpClients
.
createDefault
();
HttpPost
httpPost
=
new
HttpPost
(
config
.
getServerUrl
()
+
config
.
getUploadUrl
());
MultipartEntityBuilder
multipartEntityBuilder
=
MultipartEntityBuilder
.
create
();
multipartEntityBuilder
.
addTextBody
(
"path"
,
fullPath
);
multipartEntityBuilder
.
addBinaryBody
(
"file"
,
inputStream
);
httpPost
.
addHeader
(
HttpHeaders
.
CONTENT_TYPE
,
ContentType
.
APPLICATION_JSON
.
getMimeType
());
HttpEntity
httpEntity
=
multipartEntityBuilder
.
build
();
JSONObject
param
=
new
JSONObject
();
param
.
put
(
"path"
,
fullPath
);
param
.
put
(
"file"
,
file
.
getBytes
());
HttpEntity
httpEntity
=
new
StringEntity
(
param
.
toJSONString
(),
ContentType
.
APPLICATION_JSON
);
httpPost
.
setEntity
(
httpEntity
);
HttpResponse
httpResponse
=
httpClient
.
execute
(
httpPost
);
...
...
@@ -85,7 +100,7 @@ public class HttpFileService extends BaseService {
try
{
response
=
client
.
execute
(
httpGet
);
if
(
response
.
getStatusLine
().
getStatusCode
()
==
200
)
{
return
response
.
getEntity
().
getContent
(
);
return
IOUtils
.
toBufferedInputStream
(
response
.
getEntity
().
getContent
()
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"getUserTemplate error."
,
e
);
...
...
atms-api/src/main/java/pwc/taxtech/atms/service/impl/TemplateGroupServiceImpl.java
View file @
e84c5a33
...
...
@@ -222,7 +222,7 @@ public class TemplateGroupServiceImpl extends AbstractService {
}
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
optional
.
get
().
write
(
bos
);
String
tmpPath
=
httpFileService
.
uploadTemplate
(
newName
,
new
ByteArrayInputStream
(
bos
.
toByteArray
())
);
String
tmpPath
=
httpFileService
.
uploadTemplate
(
newName
,
file
);
String
[]
arr
=
sheetName
.
split
(
"_"
);
String
name
=
arr
.
length
>=
2
?
arr
[
1
]
:
arr
[
0
];
Template
template
=
new
Template
();
...
...
@@ -320,7 +320,7 @@ public class TemplateGroupServiceImpl extends AbstractService {
}
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
();
optional
.
get
().
write
(
bos
);
String
tmpPath
=
httpFileService
.
uploadTemplate
(
newName
,
new
ByteArrayInputStream
(
bos
.
toByteArray
())
);
String
tmpPath
=
httpFileService
.
uploadTemplate
(
newName
,
file
);
String
[]
arr
=
sheetName
.
split
(
"_"
);
String
name
=
arr
.
length
>=
2
?
arr
[
1
]
:
arr
[
0
];
Template
template
=
new
Template
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment