String ID = FieldTools.getString(request, "ID");
String file = FieldTools.getString(request, "file");
System.err.println(file);
String g_path = "";
if (!file.equals("")) {
file = StringUtil.replace(file, "\\", "/");
//指定目录
g_path = application.getRealPath("/") + "/images/";
FileInputStream in = null;
FileOutputStream out = null;
try {
g_path = g_path + ID + file.substring(file.length()-4, file.length());
g_path = g_path.replace('\\', '/');
File from = new File(file);
File to = new File(g_path);
if (!to.exists()) {
to.createNewFile();
}
int buffsize = 10240;
byte[] buffer = new byte[buffsize];
in = new FileInputStream(from);
out = new FileOutputStream(to);
int count;
while ( (count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
}
catch (Exception ex) {
Log.error(ex.getMessage());
}
finally {
try {
if (out != null) out.close();
}
catch (IOException ex1) {}
try {
if (in != null) in.close();
}
catch (IOException ex2) {}
}
}在本机上测试没有问题,但是如果局域网的机器访问我的机器(http://192.168.0.146:7001/register.jsp)的话,我在System.err.println(file)的时候显示的本机的路径,请问一下程序应该怎么改?????????????
String file = FieldTools.getString(request, "file");
System.err.println(file);
String g_path = "";
if (!file.equals("")) {
file = StringUtil.replace(file, "\\", "/");
//指定目录
g_path = application.getRealPath("/") + "/images/";
FileInputStream in = null;
FileOutputStream out = null;
try {
g_path = g_path + ID + file.substring(file.length()-4, file.length());
g_path = g_path.replace('\\', '/');
File from = new File(file);
File to = new File(g_path);
if (!to.exists()) {
to.createNewFile();
}
int buffsize = 10240;
byte[] buffer = new byte[buffsize];
in = new FileInputStream(from);
out = new FileOutputStream(to);
int count;
while ( (count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
}
catch (Exception ex) {
Log.error(ex.getMessage());
}
finally {
try {
if (out != null) out.close();
}
catch (IOException ex1) {}
try {
if (in != null) in.close();
}
catch (IOException ex2) {}
}
}在本机上测试没有问题,但是如果局域网的机器访问我的机器(http://192.168.0.146:7001/register.jsp)的话,我在System.err.println(file)的时候显示的本机的路径,请问一下程序应该怎么改?????????????
private FormFile uploadFile;
String fname=uploadFile.getFileName();
InputStream ins = uploadFile.getInputStream();
OutputStream outs=new FileOutputStream(store_path+"/"+fname);
int bytesRead=0;
int size=uploadFile.getFileSize();
byte[] buffer=new byte[size];
while((bytesRead=ins.read(buffer,0,size))!=-1){
outs.write(buffer,0,bytesRead);
}
from是File对象,当然本地可以,远程怎么找得到路经呢?
建议你用
URL u=new URL(url);
InputStream is= wavUrl.openStream();
读取流....
构造FileOutPutStream对象
System.err.println(file);
打印出来的是:C:\Documents and Settings\Administrator\桌面\2.jpg
也就是本机的地址,所以 File to = new File(g_path) 的时候虽然建立了文件,但是这个图片文件是不能预览的(length = 0 )所以楼上几位的方法好象是不能满足需求.................