gpt4 book ai didi

java - 如何使用java在tomcat webapps文件夹外读写图像

转载 作者:行者123 更新时间:2023-11-28 22:39:44 24 4
gpt4 key购买 nike

我想在 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());
}
}

}

这是我的 Controller 类

@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* 目录

谢谢

最佳答案

为了使您的代码按预期工作,您需要通过替换行来修改 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\\" 定义为全局常量(static final)变量或如果有的话,将其移动到某些设置文件中。通过这种方式,您将能够更轻松地维护您的代码,并在有一天您需要更改它时拥有可配置的图像路径。

关于java - 如何使用java在tomcat webapps文件夹外读写图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46986530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com