-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpload.java
More file actions
216 lines (205 loc) · 8.98 KB
/
Upload.java
File metadata and controls
216 lines (205 loc) · 8.98 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.secure.kk.action;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author java2
*/
public class Upload extends HttpServlet {
private static java.sql.Date getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
}
private static java.sql.Date getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Date(today.getTime());
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
Connection con;
PreparedStatement pstm = null;
String fname = "";
String keyword = "";
String cd = "";
String a = (String) request.getSession().getAttribute("email");
System.out.println("User Name : " + a);
try {
boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
if (!isMultipartContent) {
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> fields = upload.parseRequest(request);
Iterator<FileItem> it = fields.iterator();
if (!it.hasNext()) {
return;
}
while (it.hasNext()) {
FileItem fileItem = it.next();
if (fileItem.getFieldName().equals("fname")) {
fname = fileItem.getString();
System.out.println("File Name" + fname);
} else if (fileItem.getFieldName().equals("fkey")) {
keyword = fileItem.getString();
System.out.println("File Keyword" + keyword);
} else {
}
boolean isFormField = fileItem.isFormField();
if (isFormField) {
} else {
try {
con = Dbconnection.getConnection();
pstm = con.prepareStatement("insert into files (file, keyword, filetype, filename, CDate, owner, size, data, frank, file_key)values(?,?,?,?,?,?,?,?,?,?)");
System.out.println("getD " + fileItem.getName());
String str = getStringFromInputStream(fileItem.getInputStream());
// secretkey generating
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
System.out.println("secret key:" + secretKey);
//converting secretkey to String
byte[] be = secretKey.getEncoded();//encoding secretkey
String skey = Base64.encode(be);
System.out.println("converted secretkey to string:" + skey);
String cipher = new encryption().encrypt(str, secretKey);
System.out.println(str);
//for get extension from given file
String b = fileItem.getName().substring(fileItem.getName().lastIndexOf('.'));
System.out.println("File Extension" + b);
pstm.setBinaryStream(1, fileItem.getInputStream());
pstm.setString(2, keyword);
pstm.setString(3, b);
pstm.setString(4, fname);
pstm.setDate(5, getCurrentDate());
pstm.setString(6, a);
pstm.setLong(7, fileItem.getSize());
pstm.setString(8, cipher);
pstm.setString(9, "0");
pstm.setString(10, skey);
/*Cloud Start*/
File f = new File(fileItem.getName());
FileWriter fw = new FileWriter(f);
fw.write(cipher);
fw.close();
Ftpcon ftpcon = new Ftpcon();
ftpcon.upload(f, fname);
/*Cloud End*/
int i = pstm.executeUpdate();
if (i == 1) {
response.sendRedirect("owner.jsp?msg1=success");
} else {
response.sendRedirect("owner.jsp?msgg=failed");
}
con.close();
} catch (Exception e) {
out.println(e.toString());
}
}
}
} catch (FileUploadException e) {
} catch (Exception ex) {
Logger.getLogger(Upload.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
out.close();
}
}
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
}
}
}
return sb.toString();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}