在泊坞窗环境中上传和显示图片

时间:2017-01-20 18:02:21

标签: image file jsp docker

我现在遇到一些麻烦,一会儿显示存储在docker目录中的图像。

我正在dockerfile中创建目录,如下所示:

ENV IMG_PATH = / bacon / images 运行mkdir -p $ IMG_PATH 运行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

非常感谢任何有关这方面的帮助,如果有任何需要更多细节或任何内容,请告诉我们!

谢谢!

通过更改为:

来修复
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");
}   

1 个答案:

答案 0 :(得分:0)

通过更改为:

来修复
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");
}