我正在编写一个 Java EE 应用程序,我尝试从 URL 获取图像,然后将其保存在我的资源文件夹中(通过 AJAX 请求)。我的问题是,如果我不重新启动服务器,我将无法显示此图像,因为它没有加载到我的服务器上。
我正在使用 Tomcat 7、Spring、Hibernate、Primeface。
这是我保存myimage的课程
public class ImageSaver {
final static int SIZE=1024;
public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try {
URL url = new URL(fAddress);
byte[] buf;
int byteRead;
int byteWritten=0;
outStream = new BufferedOutputStream(new FileOutputStream(destinationDir+"\\"+localFileName));
uCon = url.openConnection();
is = uCon.getInputStream();
buf = new byte[SIZE];
while ((byteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
byteWritten += byteRead;
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
outStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public static void fileDownload(String fAddress,String fileName, String destinationDir){
int slashIndex =fAddress.lastIndexOf('/');
int periodIndex =fAddress.lastIndexOf('.');
//String fileName=fAddress.substring(slashIndex + 1);
if (periodIndex >=1 && slashIndex >= 0 && slashIndex < fAddress.length()-1){
}
else{
System.err.println("path or file name.");
}
}
}
以及我调用函数的方式:
ImageSaver.fileDownload("http://www.mywebsite.com/myImage.jpg","myImage.jpg", "C:\\Users\\MyProject\\src\\main\\webapp\\resources\\images");
如何在不重新启动的情况下自动加载我上传的图片?哪个文件允许配置上传文件夹?如何 ?