早教吧作业答案频道 -->其他-->
Struts2上传图片问题我把图片上传到本地项目子目录文件夹下面,在eclipse中每次要刷新文件夹才可以出来,现在把项目放到服务器上面,每次上传后,只是把文件路径保存到了数据库,而项目目录
题目详情
Struts2上传图片问题
我把图片上传到本地项目子目录文件夹下面,在eclipse中 每次要刷新文件夹才可以出来,现在把项目放到服务器上面,每次上传后,只是把文件路径保存到了数据库,而项目目录下面的文件夹,并没有上传的图片,该怎么办,
public class UploadFile {
private static final int BUFFER_SIZE = 16 * 1024;
String imageFileName;
public static InputStream in;
public static OutputStream out;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public static int getBufferSize() {
return BUFFER_SIZE;
}
private static void copy(File src, File dst) {
try {
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public String saveImage(File myFile) {
imageFileName = new Date().getTime() + getExtention(fileName);
// File imageFile = new
// File(ServletActionContext.getServletContext().getRealPath("/uploadImage")
// + "/" + imageFileName);
File imageFile = new File("D:/java/workspaces/mjms/WebRoot/image" + "/"
+ imageFileName);
copy(myFile, imageFile);
return imageFileName;
}
}
我把图片上传到本地项目子目录文件夹下面,在eclipse中 每次要刷新文件夹才可以出来,现在把项目放到服务器上面,每次上传后,只是把文件路径保存到了数据库,而项目目录下面的文件夹,并没有上传的图片,该怎么办,
public class UploadFile {
private static final int BUFFER_SIZE = 16 * 1024;
String imageFileName;
public static InputStream in;
public static OutputStream out;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public static int getBufferSize() {
return BUFFER_SIZE;
}
private static void copy(File src, File dst) {
try {
try {
in = new BufferedInputStream(new FileInputStream(src),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
public String saveImage(File myFile) {
imageFileName = new Date().getTime() + getExtention(fileName);
// File imageFile = new
// File(ServletActionContext.getServletContext().getRealPath("/uploadImage")
// + "/" + imageFileName);
File imageFile = new File("D:/java/workspaces/mjms/WebRoot/image" + "/"
+ imageFileName);
copy(myFile, imageFile);
return imageFileName;
}
}
▼优质解答
答案和解析
File imageFile = new File("D:/java/workspaces/mjms/WebRoot/image" + "/"
+ imageFileName);
服务器上没有这个文件,应该是这里的路径,这是你本机的路径,在服务器上有这个目录?建议写相对路径.
还有个问题,就是你说的每次要刷新的问题,
问题在这里:
if (null != out) {
out.frush();//加上这一句代码,就不用手动刷新了
out.close();
}
+ imageFileName);
服务器上没有这个文件,应该是这里的路径,这是你本机的路径,在服务器上有这个目录?建议写相对路径.
还有个问题,就是你说的每次要刷新的问题,
问题在这里:
if (null != out) {
out.frush();//加上这一句代码,就不用手动刷新了
out.close();
}
看了Struts2上传图片问题我把...的网友还看了以下:
最基础一次函数下列说法不正确的是1.公式V=4/3πr^3中,4/3和π是常量,r是自变量,V是r的 2020-03-31 …
无氧酸有那些,分解反应有水和盐生成的第一问,可用元素CU,C(+4),O,H,NA,S(-2)朋友 2020-05-17 …
留数定理求待定系数X(s)=分子:3s^2+2s+8.分母:s(s+2)(s^2+2s+4).(s 2020-06-04 …
多重集排列S={2*a,2*b},求S的2-排列,我想问一下,结果应当是aa,bb,ab,ba,一 2020-06-08 …
用matlab解Lorenz微分方程组的问题dx/dt=a(y-x)dy/dt=rx-y-xzdz 2020-07-24 …
设A=(α1,α2,…,αr)是n×r矩阵,B=(β1,β2,…,βs)是n×s矩阵,rank(A) 2020-11-01 …
如何用另一种方式表达:已知s是由实数组成的集合,且满足1:不属于s,2:若a属于s,则1-a分之1属 2020-11-01 …
我发现物理计算中单位可以计算,并且所有公式都符合,好像有个东西叫纲量,例如加速度是m*s^-2F=m 2020-11-03 …
设向量组(Ⅰ)α1,α2,…,αs,其秩为r1,向量组(Ⅱ)β1,β2,…,βs,其秩为r2,且βi 2020-11-11 …
一个位移和加速度的问题s=v.t+0.5at平方当加速度a为负数时比如-2吧s=v.t-t平方那随着 2020-11-24 …