我写的程序,系统Start后,执行上传没有任何错误,如果是同局域网的其他机器来访问的话,那么就会报错:“java.io.FileNotFoundException: d:\我的文档\桌面\123.txt (系统找不到指定的文件。)” 这样的类似的错误,我想我写的代码是不管怎么样都会读取我本机的路径下文件,而不会去读取访问我系统人的机器上文件代码如下:import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;import com.sjedu.service.test.UpLoad;public class UploadFileAction { // log4j日志
private static final Logger logger = Logger.getLogger(UploadFileAction.class);
private String jRandomid;// 页面随机数,防止代理等应用缓存页面信息
private static final int BUFFER_SIZE = 16 * 1024 ;
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
String filename=request.getParameter("filename");
String ofile=request.getParameter("filename");
int t1 = filename.lastIndexOf("\\");
int t2 = filename.lastIndexOf(".");
if(t1 >= 0 && t1 < t2 && t1 < filename.length()){
filename= filename.substring(t1 + 1, filename.length());
}
int t3 = ofile.lastIndexOf(".");
String fileExt=ofile.substring(t3 + 1, ofile.length());
System.out.println("fileExt---"+fileExt);
System.out.println(filename);
//输入流
InputStream in =new FileInputStream(ofile);
int len=in.available();
//获取file路径;
//获取新文件名;
String newDateStr=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).toString();//获取前17位
String newRandom="";//获取后三位随机数
int newInt=(int)(Math.random()*1000);
if(newInt<100){
newRandom="0"+String.valueOf(newInt);
}else
newRandom=String.valueOf(newInt);
String newFileName=newDateStr+newRandom+"."+fileExt;
File imageFile = new File(request.getSession().getServletContext().getRealPath("/")+newFileName);
//上传文件
copy(new File(ofile), imageFile);
PrintWriter pw = response.getWriter();
//返回数据(原文件名|新文件名|文件大小|后缀名|)
pw.write(filename+"|"+newFileName+"|"+len+"|"+fileExt);
return null;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
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();
}
}
public String getJRandomid() {
return jRandomid;
}
public void setJRandomid(String randomid) {
jRandomid = randomid;
}
}
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;import com.sjedu.service.test.UpLoad;public class UploadFileAction { // log4j日志
private static final Logger logger = Logger.getLogger(UploadFileAction.class);
private String jRandomid;// 页面随机数,防止代理等应用缓存页面信息
private static final int BUFFER_SIZE = 16 * 1024 ;
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
String filename=request.getParameter("filename");
String ofile=request.getParameter("filename");
int t1 = filename.lastIndexOf("\\");
int t2 = filename.lastIndexOf(".");
if(t1 >= 0 && t1 < t2 && t1 < filename.length()){
filename= filename.substring(t1 + 1, filename.length());
}
int t3 = ofile.lastIndexOf(".");
String fileExt=ofile.substring(t3 + 1, ofile.length());
System.out.println("fileExt---"+fileExt);
System.out.println(filename);
//输入流
InputStream in =new FileInputStream(ofile);
int len=in.available();
//获取file路径;
//获取新文件名;
String newDateStr=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).toString();//获取前17位
String newRandom="";//获取后三位随机数
int newInt=(int)(Math.random()*1000);
if(newInt<100){
newRandom="0"+String.valueOf(newInt);
}else
newRandom=String.valueOf(newInt);
String newFileName=newDateStr+newRandom+"."+fileExt;
File imageFile = new File(request.getSession().getServletContext().getRealPath("/")+newFileName);
//上传文件
copy(new File(ofile), imageFile);
PrintWriter pw = response.getWriter();
//返回数据(原文件名|新文件名|文件大小|后缀名|)
pw.write(filename+"|"+newFileName+"|"+len+"|"+fileExt);
return null;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
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();
}
}
public String getJRandomid() {
return jRandomid;
}
public void setJRandomid(String randomid) {
jRandomid = randomid;
}
}
解决方案 »
- hibernate 懒加载问题
- java新闻抓取程序图片下载不全的问题
- myeclipse中,添加hibernate时 db profile为空
- 哪位仁兄又weblogic的中文教程
- 现在有人用Java开发科学计算类型的系统么?
- 紧急求助,大家帮我看看这是个什么错误
- 在Struts架构下前台同后台传参数,为什么这样做不行呢?
- jbuilder x + weblogic8.1,运行一个struts的jsp页面报错,请帮帮忙
- 如何在struts结构的jsp页面上向数据库上载大于1M的文件
- 怎样通过调用findAll()将数据库中的字段全部取出!
- 求救 struts2中的select标签
- hibernate 批量删除问题
String fileExt=ofile.substring(t3 + 1, ofile.length());
System.out.println("fileExt---"+fileExt);
System.out.println(filename);
//输入流
InputStream in =new FileInputStream(ofile);
int len=in.available();
要是这么简单,就不需要apache 的commonfileupload.jar了
import java.io.File;
import java.io.IOException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class test extends ActionSupport{
private File file;//添加一个file属性 public File getFile() {
return file;
} public void setFile(File file) {
this.file = file;
} public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
HttpServletResponse response = ServletActionContext.getResponse();
String destDir = request.getSession().getServletContext().getRealPath("/")+ System.currentTimeMillis();
try {
FileUtils.copyFile(file, new File(destDir));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}}
这个例子就是 你说的意思吧,我看了 在局域网的机器上访问我所做系统,执行,还是没写入 而且我点了上传按钮后 要求在本页面显示出刚上传的文件名
子窗口给父窗口返回值。
但是子窗口上传文件结束又如何给父窗口返回值呢?我想应该是子窗口里在加个iframe,把上传的target设置在这个iframe上,这个iframe得到结果再去更新父窗口。不知道是不是这个意思。