如何使用java在tomcat webapps文件夹外读取和写入Image

时间:2017-10-28 05:20:15

标签: java spring tomcat primefaces

我想在tomcat之外的本地pc文件夹(如E:/ CustomImage / Images /)中保存和检索图像。

这是我的服务类

public class ImgUpload_Impl implements Serializable{
private static final long serialVersionUID = 1L;

 public void uploadImg(String imgPath, String fileName,UploadedFile uploadFile){
 ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String filepath = servletContext.getRealPath("/Images/"+imgPath) + File.separator + fileName;
try
   {
    InputStream is;
   try (FileOutputStream fos = new FileOutputStream(new File(filepath))) {
    is = uploadFile.getInputstream();
            int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];
            int a;
            while (true) {
            a = is.read(buffer);

                if (a < 0) {
                    break;
                }

                fos.write(buffer, 0, a);

                fos.flush();
            }  
        }
       is.close();
      } 
    catch (IOException e)
    {
        System.out.println("ImgUpload_Impl : uploadImg : "+e.getMessage());
    }
 }

}

这是我的控制器类

@ManagedBean
@Scope("session")
@Controller(value = "studentMB")
public class StudentMB{
private UploadedFile uploadedFile;
ImgUpLoad servicedao = new ImgUpload_Impl();

public void doRegistration(){
try{
 boolean imageContentType=servicedao.checkImageContenType(this.uploadedFile);
  if (!this.uploadedFile.getFileName().equals("") && imageContentType) {
  String imageFileName=stdRegBasicInfo.getStudentID()+"_stdimg.jpg";
   this.stdRegBasicInfo.setStudentName(imageFileName);//Here image name are save in database
    servicedao.uploadImgHeightWidth200("studentImage", imageFileName, this.uploadedFile);
 }
}catch(Exception e){
e.printStackTrace();
}
}

public UploadedFile getUploadedFile() {
    return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile) {
    this.uploadedFile = uploadedFile;
}

}

这是查看页面

 <ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/template/template.xhtml">

<ui:define name="content">
    <h:form>
        <p:outputLabel value="Student Image" />
        <p:fileUpload value="#{studentMB.uploadedFile}" skinSimple="true" 
          allowTypes="/(\.|\/)(gif|jpe?g|png)$/" mode="simple" />

        <p:commandButton value="Save" styleClass="btnSave" ajax="false"
            style="margin-left:12px" action="#{studentMB.doRegistration}"
            update="singlestd" icon="fa fa-save" />
    </h:form>
</ui:define>

当我上传并保存图片时,请先保存图片 E:\ Eduman.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ wtpwebapps \ EduManager4 \ Images \ studentImage \ 100122066717_stdimg.jpg 目录

但我想保存图片 * E:\ CustomImage \ Images * 目录

由于

1 个答案:

答案 0 :(得分:1)

要使代码按预期工作,您需要通过替换行

来修改class ImgUpload_Impl
String filepath = servletContext.getRealPath("/Images/"+imgPath) + File.separator + fileName;

这一个

String filepath = "E:\\CustomImage\\Images\\"+imgPath + File.separator + fileName;

不要忘记确保文件夹 E:\ CustomImage \ Images \ studentImage 确实存在。如果它不存在,则从您的代码手动或以编程方式创建它。

由于您可能需要稍后阅读上传的图像,我建议您将字符串"E:\\CustomImage\\Images\\"定义为全局常量(静态最终)变量,或者如果您拥有它,则将其移动到某些设置文件中。 通过这种方式,您可以更轻松地维护代码,并且如果有一天您需要更改它,则可以使用可配置的图像路径。

相关问题