1、 在web.xml中配置文件上传的路径
<!-- 上传路径 -->
<context-param>
<param-name>rootpath</param-name>
<param-value>D://test</param-value>
</context-param>
2、获得web.xml中配置的文件目录
String rootpath = request.getSession().getServletContext().getInitParameter("rootpath");
3、
/**
* 创建目录
* @param path
*/
public static void mkRootDir(String path){
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
}
4、/**
* 拷贝文件
* @param copyPath 目标路径
* @param inputStream 源文件流
* @throws Exception
*/
public static void copyFile(String copyPath, InputStream inputStream) throws Exception {
File copyFile = new File(copyPath);
if(copyFile.isDirectory())
if(false == copyFile.exists())
copyFile.mkdirs();
if (copyFile.isFile()==false) {
copyFile.createNewFile();
}
OutputStream outputStream = new FileOutputStream(copyFile);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
}
5、