Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
U
UploadingFile
Project
Project
Details
Activity
Releases
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
UploadingFile
Commits
8c405c23
Commit
8c405c23
authored
Nov 23, 2023
by
abdullh.alsoleman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
REST_gRPC
parent
690643b0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
0 additions
and
121 deletions
+0
-121
.gitkeep
UploadingFile_Client/.gitkeep
+0
-0
FileUploadController.java
...lient/src/main/java/Controllers/FileUploadController.java
+0
-19
UploadFileService.java
...File_Client/src/main/java/Services/UploadFileService.java
+0
-102
التقرير.pdf
التقرير.pdf
+0
-0
No files found.
UploadingFile_Client/.gitkeep
deleted
100644 → 0
View file @
690643b0
UploadingFile_Client/src/main/java/Controllers/FileUploadController.java
deleted
100644 → 0
View file @
690643b0
package
Controllers
;
import
Services.UploadFileService
;
import
org.springframework.http.MediaType
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
@RestController
public
class
FileUploadController
{
private
final
UploadFileService
uploadFileService
;
public
FileUploadController
(
UploadFileService
uploadFileService
)
{
this
.
uploadFileService
=
uploadFileService
;
}
@PostMapping
(
value
=
"/upload"
,
consumes
=
MediaType
.
MULTIPART_FORM_DATA_VALUE
)
public
String
uploadFile
(
@RequestParam
(
"file"
)
MultipartFile
file
)
{
return
this
.
uploadFileService
.
uploadFile
(
file
);
}
}
UploadingFile_Client/src/main/java/Services/UploadFileService.java
deleted
100644 → 0
View file @
690643b0
package
Services
;
import
Uploading_Service.*
;
import
com.google.protobuf.ByteString
;
import
io.grpc.Metadata
;
import
io.grpc.stub.MetadataUtils
;
import
io.grpc.stub.StreamObserver
;
import
net.devh.boot.grpc.client.inject.GrpcClient
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.util.concurrent.CountDownLatch
;
@Service
public
class
UploadFileService
{
//Asynchronous stub of client
private
final
uploadingServiceGrpc
.
uploadingServiceStub
client
;
// @GrpcClient is help annotation refer to which server it requires to call
//file-upload is the key will be read from properties file
public
UploadFileService
(
@GrpcClient
(
value
=
"file-upload"
)
uploadingServiceGrpc
.
uploadingServiceStub
client
)
{
this
.
client
=
client
;
}
public
String
uploadFile
(
final
MultipartFile
multipartFile
)
{
//file name
String
name
;
//file size
int
size
;
//to read the bytes
InputStream
inputStream
;
name
=
multipartFile
.
getOriginalFilename
();
try
{
size
=
multipartFile
.
getBytes
().
length
;
inputStream
=
multipartFile
.
getInputStream
();
}
catch
(
IOException
e
)
{
return
"unable to extract file info"
;
}
StringBuilder
response
=
new
StringBuilder
();
Metadata
metadata
=
new
Metadata
();
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
1
);
metadata
.
put
(
Metadata
.
Key
.
of
(
"file-meta-bin"
,
Metadata
.
BINARY_BYTE_MARSHALLER
),
FileMetadata
.
newBuilder
()
.
setFileNameWithType
(
name
)
.
setContentLength
(
size
)
.
build
()
.
toByteArray
());
//using this object we will stream the file content to the server
StreamObserver
<
FileUploadRequest
>
fileUploadRequestStreamObserver
=
this
.
client
.
withInterceptors
(
MetadataUtils
.
newAttachHeadersInterceptor
(
metadata
))
.
uploadFile
(
new
StreamObserver
<
FileUploadResponse
>()
{
@Override
//called when server sends the response
public
void
onNext
(
FileUploadResponse
fileUploadResponse
)
{
response
.
append
(
fileUploadResponse
.
getUploadStatus
());
}
@Override
//called when server send an error
public
void
onError
(
Throwable
throwable
)
{
response
.
append
(
UploadStatus
.
FAILED
);
throwable
.
printStackTrace
();
countDownLatch
.
countDown
();
}
@Override
//called when server finish processing the request
public
void
onCompleted
()
{
countDownLatch
.
countDown
();
}
});
//chunk data, i use the size 10 KB for each chunk
byte
[]
tenKB
=
new
byte
[
10240
];
int
length
;
try
{
while
((
length
=
inputStream
.
read
(
tenKB
))
>
0
)
{
FileUploadRequest
request
=
FileUploadRequest
.
newBuilder
()
.
setFile
(
File
.
newBuilder
().
setContent
(
ByteString
.
copyFrom
(
tenKB
,
0
,
length
)))
.
build
();
//sending the request that contain the chunk data of file
fileUploadRequestStreamObserver
.
onNext
(
request
);
}
inputStream
.
close
();
//notify the server we have completed sending the chunk data
fileUploadRequestStreamObserver
.
onCompleted
();
//waiting server response
countDownLatch
.
await
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
response
.
append
(
UploadStatus
.
FAILED
);
}
return
response
.
toString
();
}
}
التقرير.pdf
View file @
8c405c23
No preview for this file type
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