亲们,关于文件复制的问题,帮帮我 我想把本地磁盘比如D:/text.doc放到我的web项目中的webroot下的content文件夹中,当然是通过程序拷贝过去,不是直接copy过去,我是通过路径拷贝的,可怎么也考不进去,我的路径是request.getContextPath()+"content";不知大家有没有好的方法,非常感谢大家帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 request.getContextPath()+"/content"; System.out.println输出路径。 向二楼所说的那样,改了,错误提示:FileNotFoundException:\wmdw\content(系统找不到指定路径)wmdw是我的项目名称 把我的main方法挪到你的action中,路径换下试试:import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;public class Test { /** * 复制文件。targetFile为目标文件,file为源文件 * * @param targetFile * @param file */ public static void copyFile(File targetFile, File file) { if (targetFile.exists()) { System.out.println("文件" + targetFile.getAbsolutePath() + "已经存在,跳过该文件!"); return; } else { createFile(targetFile, true); } System.out.println("复制文件" + file.getAbsolutePath() + "到" + targetFile.getAbsolutePath()); try { InputStream is = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(targetFile); byte[] buffer = new byte[1024]; while (is.read(buffer) != -1) { fos.write(buffer); } is.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * createFile * * @param file * @param isFile * * @author guoqiang <[email protected]> */ public static void createFile(File file, boolean isFile) { if (!file.exists()) { if (!file.getParentFile().exists()) { createFile(file.getParentFile(), false); } else { if (isFile) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } else { file.mkdir(); } } } } /** * main * * @param args * * @author guoqiang <[email protected]> */ public static void main(String[] args) { // 源文件 File initFile = new File("D:/test/1item.txt"); // 目标文件 File targetFile = new File("D:/test2/1item.txt"); copyFile(targetFile, initFile); }} 关键点式获取web应用的绝对路径。 String projectAbsolutePath = System.getProperty("user.dir");这是获取当前工程路径的代码,工程中的webroot下的content文件夹路径是固定的,自己加上就OK了。 WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()).getServletContext().getRealPath("");我以前是用这个获取绝对路径的 js问题 帮帮忙 请大家帮我看哈,关于session的问题 求JSP项目 jasper报表-斜线怎么显示 急~~急~~急~~关于weblogic从8.1升级到weblogic9.2的问题 Jode要怎么使用? jsp中如何实现这样的流程(关于打开新窗口) junit 测试报错 servlet的安全机制(@ServletSecurity)问题 ssm框架中在页面上修改完数据后数据库数据没有被修改 hibernate使用jndi报错如何解决 "会者不难,难者不会"的面试题
System.out.println输出路径。
wmdw是我的项目名称
把我的main方法挪到你的action中,路径换下试试:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;public class Test { /**
* 复制文件。targetFile为目标文件,file为源文件
*
* @param targetFile
* @param file
*/
public static void copyFile(File targetFile, File file) { if (targetFile.exists()) {
System.out.println("文件" + targetFile.getAbsolutePath() + "已经存在,跳过该文件!");
return;
} else {
createFile(targetFile, true);
}
System.out.println("复制文件" + file.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
fos.write(buffer);
}
is.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* createFile
*
* @param file
* @param isFile
*
* @author guoqiang <[email protected]>
*/
public static void createFile(File file, boolean isFile) { if (!file.exists()) {
if (!file.getParentFile().exists()) {
createFile(file.getParentFile(), false);
} else {
if (isFile) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();
}
}
}
} /**
* main
*
* @param args
*
* @author guoqiang <[email protected]>
*/
public static void main(String[] args) { // 源文件
File initFile = new File("D:/test/1item.txt"); // 目标文件
File targetFile = new File("D:/test2/1item.txt"); copyFile(targetFile, initFile);
}
}
这是获取当前工程路径的代码,工程中的webroot下的content文件夹路径是固定的,自己加上就OK了。