2017-01-20 3 views
0

У меня возникли некоторые проблемы сейчас, когда я получаю изображение для отображения, которое хранится внутри каталога докеров.Загрузка и отображение изображений в среде докеров

Я создаю каталог внутри dockerfile так:

ENV IMG_PATH =/бекон/изображения MkDir -p RUN $ IMG_PATH RUN CHMOD -R 777 $ IMG_PATH

код, который я есть который используется, чтобы сохранить файл в директории:

<%@ page import="java.io.*,java.sql.*,java.util.zip.*" %> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 

     <% 
String saveFile=""; 
String contentType = request.getContentType(); 
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){ 
DataInputStream in = new DataInputStream(request.getInputStream()); //input stream 
int formDataLength = request.getContentLength(); 
out.println("Form Data Length: " +formDataLength); 
byte dataBytes[] = new byte[formDataLength]; 
int byteRead = 0; 
int totalBytesRead = 0; 
while(totalBytesRead < formDataLength){ 
byteRead = in.read(dataBytes, totalBytesRead,formDataLength); 
totalBytesRead += byteRead; 
} 
String file = new String(dataBytes,"CP1256"); 
saveFile = file.substring(file.indexOf("filename=\"") + 10); 
out.println("Save File: " +saveFile); 
saveFile = saveFile.substring(0, saveFile.indexOf("\n")); 
out.println("Save File: " +saveFile); 
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\"")); 
out.println("Save File: " +saveFile); 
int lastIndex = contentType.lastIndexOf("="); 
String boundary = contentType.substring(lastIndex + 1,contentType.length()); 
int pos; 
pos = file.indexOf("filename=\""); 
pos = file.indexOf("\n", pos) + 1; 
pos = file.indexOf("\n", pos) + 1; 
pos = file.indexOf("\n", pos) + 1; 
int boundaryLocation = file.indexOf(boundary, pos) - 4; 
int startPos = ((file.substring(0, pos)).getBytes("CP1256")).length; 
int endPos = ((file.substring(0, boundaryLocation)).getBytes("CP1256")).length; 

File ff = new File("/bacon/images/"+saveFile); 
FileOutputStream fileOut = new FileOutputStream(ff); 
fileOut.write(dataBytes, startPos, (endPos - startPos)); 
fileOut.flush(); 
fileOut.close(); 
} 
%> 

Я уверен, что это работает, как при тестировании с:

File f = new File("/bacon/images/"+imageId); 
out.println(f.isFile()); // --> returns boolean 

Что возвращает значение true после загрузки файла.

Я пытаюсь отобразить изображение с:

<img class="thumbnail img-responsive" alt="Bootstrap template" src="getImage.jsp?imageId=1 (1).png"> 

Который называет:

<%@page contentType="text/html" pageEncoding="windows-1252"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <%@page import="java.io.File, org.apache.commons.codec.binary.Base64,org.apache.commons.io.FileUtils, java.io.IOException"%> 

     <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <% 
String imageId = request.getParameter("imageId"); 
File f = new File("/bacon/images/"+imageId); 
try{ 
byte[] pictureBytes = Base64.encodeBase64(FileUtils.readFileToByteArray(f)); 
response.setContentType("image/png"); 
response.getOutputStream().write(pictureBytes); 
} 
catch (IOException e) { 
    System.err.println("Caught IOException: " + e.getMessage()); 
} 
response.getOutputStream().flush(); 
response.getOutputStream().close(); 

     %> 
    </body> 
</html> 

Что является дисплей: https://www.screencast.com/t/SgQFsKkKdX2s

Давать 200 ладно: 200 okay

Любая помощь по этому поводу Я бы очень признателен, дайте мне знать, нужны ли какие-либо бит больше деталей или чего-то еще!

Спасибо!

Fixed путем перехода к:

String imageId = request.getParameter("imageId"); 
File f = new File("/bacon/images/"+imageId); 
//out.println(f.isFile()); 
try{ 
//byte[] pictureBytes = Base64.encodeBase64(FileUtils.readFileToByteArray(f)); 
byte[] pictureBytes = Files.readAllBytes(f.toPath()); 
response.setContentType("image/jpeg"); 
response.setContentLength(pictureBytes.length); // imageBytes - image in bytes 
response.getOutputStream().write(pictureBytes); 
response.getOutputStream().flush(); 
response.getOutputStream().close(); 
} 
catch (IOException e) { 
    System.err.println("Caught IOException: " + e.getMessage()); 
    out.println("error"); 
} 
+0

Могли бы вы предоставить ваш Dockerfile? –

ответ

0

Fixed путем перехода к:

String imageId = request.getParameter("imageId"); 
File f = new File("/bacon/images/"+imageId); 
//out.println(f.isFile()); 
try{ 
//byte[] pictureBytes = Base64.encodeBase64(FileUtils.readFileToByteArray(f)); 
byte[] pictureBytes = Files.readAllBytes(f.toPath()); 
response.setContentType("image/jpeg"); 
response.setContentLength(pictureBytes.length); // imageBytes - image in bytes 
response.getOutputStream().write(pictureBytes); 
response.getOutputStream().flush(); 
response.getOutputStream().close(); 
} 
catch (IOException e) { 
    System.err.println("Caught IOException: " + e.getMessage()); 
    out.println("error"); 
}