遇到一个很棘手问题~
用户上传自己的logo
String filePath1 = servlet.getServletContext().getRealPath("/img"); 获取到tomcat下img包的路径
fileOutput = new FileOutputStream(filePath1 + "\\" + logoUrlPath.replace(" ",""));
fileOutput.write(logoUrl.getFileData());
fileOutput.flush();
fileOutput.close();用数据流上传成功~!可是等我第二天来的时候这张图片就不见了(就和删除了一样,数据库存入的是这张图片的路径依然存在) 不知道是什么问题? 反复试验了几次都这样 请教各位这是什么问题造成的? 如何解决?
用户上传自己的logo
String filePath1 = servlet.getServletContext().getRealPath("/img"); 获取到tomcat下img包的路径
fileOutput = new FileOutputStream(filePath1 + "\\" + logoUrlPath.replace(" ",""));
fileOutput.write(logoUrl.getFileData());
fileOutput.flush();
fileOutput.close();用数据流上传成功~!可是等我第二天来的时候这张图片就不见了(就和删除了一样,数据库存入的是这张图片的路径依然存在) 不知道是什么问题? 反复试验了几次都这样 请教各位这是什么问题造成的? 如何解决?
// 上传图片-------------开始------------
Date date = new Date(); // FormFile用于指定存取文件的类型
FormFile file = productsaddForm.getFile(); // 获取当前的文件 // 获得系统的绝对路径 String dir =
// servlet.getServletContext().getRealPath("/image");
// 我上传的文件没有放在服务器上。而是存在D:D:\\loadfile\\temp\\
String dir = "/qqQBS_admin/upload"; int i = 0;
String type = file.getFileName();
while (i != -1) {
// 找到上传文件的类型的位置,这个地方的是'.'
i = type.indexOf(".");
/* System.out.println(i); */
/* 截取上传文件的后缀名,此时得到了文件的类型 */
type = type.substring(i + 1);
}
// 限制上传类型为jpg,txt,rar; if (1 == 1) {
// 将上传时间加入文件名(这个地方的是毫秒数)
String times = String.valueOf(date.getTime());
// 组合成 time.type
String fname = times + "." + type;
// InInputStream是用以从特定的资源读取字节的方法。
InputStream streamIn = file.getInputStream(); // 创建读取用户上传文件的对象
// 得到是字节数,即byte,我们可以直接用file.getFileSize(),也可以在创建读取对象时用streamIn.available();
// int ok=streamIn.available();
int ok = file.getFileSize();
String strFee = null;
// 这个地方是处理上传的为M单位计算时,下一个是以kb,在下一个是byte;
if (ok >= 1024 * 1024) {
float ok1 = (((float) ok) / 1024f / 1024f);
DecimalFormat myformat1 = new DecimalFormat("0.00");
strFee = myformat1.format(ok1) + "M"; } else if (ok > 1024 && ok <= 1024 * 1024) {
double ok2 = ((double) ok) / 1024;
DecimalFormat myformat2 = new DecimalFormat("0.00");
strFee = myformat2.format(ok2) + "kb"; } else if (ok < 1024) {
strFee = String.valueOf(ok) + "byte";
} // 这个是io包下的上传文件类
File uploadFile = new File(dir); // 指定上传文件的位置
if (!uploadFile.exists() || uploadFile == null) { // 判断指定路径dir是否存在,不存在则创建路径
uploadFile.mkdirs();
}
// 上传的路径+文件名
String path = uploadFile.getPath() + "/" + fname;
// OutputStream用于向某个目标写入字节的抽象类,这个地方写入目标是path,通过输出流FileOutputStream去写
OutputStream streamOut = new FileOutputStream(path);
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 将数据读入byte数组的一部分,其中读入字节数的最大值是8192,读入的字节将存储到,buffer[0]到buffer[0+8190-1]的部分中
// streamIn.read方法返回的是实际读取字节数目.如果读到末尾则返回-1.如果bytesRead返回为0则表示没有读取任何字节。
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
// 写入buffer数组的一部分,从buf[0]开始写入并写入bytesRead个字节,这个write方法将发生阻塞直至字节写入完成。
streamOut.write(buffer, 0, bytesRead);
}
// 关闭输出输入流,销毁File流。
streamOut.close();
streamIn.close();
file.destroy();
String paths = path; String fileName = productsaddForm.getFile().getFileName(); // 获取文件的名称
// String fileSize = String.valueOf(file.getFileSize());
String fileDate = DateFormat.getDateInstance().format(date);
if (fname.indexOf("jif") == -1 && fname.indexOf("jpg") == -1) {
prodBean.setProImgPath("/images img.gif");
} else {
prodBean.setProImgPath("/qqQBS_admin/upload/" + fname);
}
prodImpl.save(prodBean);
}
return mapping.findForward("ok");
}