-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudStorageServlet.java
More file actions
81 lines (68 loc) · 2.33 KB
/
Copy pathCloudStorageServlet.java
File metadata and controls
81 lines (68 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// BUSINESS LOGIC LAYER
//
package edu.bits.cloud.ec3;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class CloudStorageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private StorageService storage = new StorageService();
private static final int BUFFER_SIZE = 1024 * 1024;
private static final Logger log = Logger
.getLogger(CloudStorageServlet.class.getName());
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
log.info(this.getServletInfo() + " Servlets called....");
resp.setContentType("text/plain");
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter;
try {
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fileName = item.getName();
String mime = item.getContentType();
storage.init(fileName, mime);
InputStream is = item.openStream();
byte[] b = new byte[BUFFER_SIZE];
int readBytes = is.read(b, 0, BUFFER_SIZE);
boolean isUploadFailed = false;
try {
while (readBytes != -1) {
storage.storeFile(b, readBytes);
readBytes = is.read(b, 0, readBytes);
}
} catch (IOException ie) {
isUploadFailed = true;
} finally {
is.close();
storage.destroy();
}
if(isUploadFailed) {
resp.getWriter().println("File upload failed...");
} else {
resp.getWriter().println("File uploading done successfully");
}
log.info(this.getServletName() + " ended....");
}
} catch (FileUploadException e) {
System.out.println("FileUploadException::" + e.getMessage());
log.severe(this.getServletName() + ":FileUploadException::"
+ e.getMessage());
e.printStackTrace();
} catch (Exception e) {
log.severe(this.getServletName() + ":Exception::" + e.getMessage());
System.out.println("Exception::" + e.getMessage());
e.printStackTrace();
}
}
}