I am allowing the user to upload files to imgur hosting and get a link back. Everything seems to be working properly. However I am using
String itemName = item.getName();
It works perfectly in Internet Explorer and the browser of Eclipse. However when it comes to firefox and chrome it doesn't since due to browser security only the file name is retrieved in the input field. What is the workaround to get it to work?
My code:-
private void processlist(HttpServletRequest request,
HttpServletResponse response) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
} else {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
} else {
try {
String itemName = item.getName();
BufferedImage img = null;
try {
img = ImageIO.read(new File(itemName));
} catch (IOException e) {
}
String IMGUR_POST_URI = "https://api.imgur.com/3/upload";
String IMGUR_API_KEY = "mykeyyyyyyyyyyyy";
String projectname = "";
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("Writing image...");
ImageIO.write(img, "png", baos);
URL url = new URL(IMGUR_POST_URI);
System.out.println("Encoding...");
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder
.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "="
+ URLEncoder.encode(IMGUR_API_KEY, "UTF-8");
System.out.println("Connecting...");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
System.out.println("Sending data...");
wr.write(data);
wr.flush();
System.out.println("Finished.");
// just display the raw response
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONObject jObject = new JSONObject(line);
JSONObject data1 = jObject.getJSONObject("data");
projectname = data1.getString("link");
if (flagAccessed == false) {
img1 = projectname;
} else {
img2 = projectname;
}
System.out.print(projectname);
My JSP code is a simple form:-
<form action="UploadServlet" method="post" enctype="multipart/form-data" name="form1" id="form1" style="text-align:center;display:inline;">
<table border="1" style="text-align:center;">
<td><center>
Select image1: <input name="file" type="file" id="file" class="btn btn-dark btn-lg12">
</center></td>
</tr>
<tr>
<td><center>
Select image2: <input name="file" type="file" id="file" class="btn btn-dark btn-lg12">
</center></td>
</tr>
<tr>
<td align="center" style="margin-top: 5px;"></td>
</tr>
</table>
<br>
<br>
<input type="submit" name="Submit" value="Submit files" class="btn btn-dark btn-lg"/>
</form>