乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > java怎么创建新文件到webroot文件下

java怎么创建新文件到webroot文件下

作者:乔山办公网日期:

返回目录:excel表格制作


放在webContent或者叫webRoot下,部署的时候这个文件夹会放到服务器上

如果用工具,那就鼠标点到webroot,右键,新建文件夹即可,如果使用程序,则为

String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
System.out.println(savePath);
//在/WEB-INF下创建一个upload名字的文件
File file = new File(savePath);
        if(!file.isDirectory() && !file.exists()){
            System.out.println("目录或文件不存在!");
            file.mkdir();
        }

首先你这个方法请求过来,你将这个aa.XLS 读取到内存中,再以流的方式写出去就行了
//定义EXCEL文档
HSSFWorkbook importExcelBook = null;
InputStream is = null;
//获取指定地址下的EXELE文档
File file = new File(“路径");
try
e79fa5e98193e58685e5aeb9363{
is = new FileInputStream(file);

importExcelBook = new HSSFWorkbook(is);
}
catch (Exception e)
{
throw new BusinessException("获取文件失败");
}

if(null != importExcelBook)
{
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition",
"attachment; filename=\"" + "backToBackTemplate.xls");
OutputStream os = null;

try
{
os = response.getOutputStream();
importExcelBook.write(os);
os.flush();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if(null != os)
{
try
{
os.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}

}
}
大概就是这样

不知道你到底想要将你的文件话到什么地方,如果你是用tomcat的话,可以扩展org.apache.naming.resources.FileDirContext类(${TOMCAT_HOME}/lib/catalina.jar包下),然后在server.xml中进行配置,这样的话,你可以将你的文件放到任何你想放的地方,也不需要你写servlet(当然了,e799bee5baa6e997aee7ad94e78988e69d83337这只是你不需要做特别处理的情况下)。

扩展样例代码:
package zhangyw.test.tomcat;

import java.io.File;

import org.apache.naming.resources.FileDirContext;

public class VirtualFileDirContext extends FileDirContext {

private String directory = null;

public void setDirectory(final String directory) {
this.directory = directory;
}

@Override
protected File file(final String name) {
File file = super.file(name);
if (file != null) {
return file;
}

file = new File(directory, name);
return file;
}

}

server.xml中的配置样例
<Context docBase="tomcatweb" path="/tomcatweb" reloadable="true" source="org.eclipse.jst.jee.server:tomcatweb">
<Resources className="zhangyw.test.tomcat.VirtualFileDirContext" directory="D:\Books"/>
</Context>

使用样例
http://localhost:8080/tomcatweb/php_manual_zh.chm
其中,tomcatweb是当前web应用的上下文,php_manual_zh.chm在文件系统中的实际路径为"D:\Books\php_manual_zh.chm"。

以上,希望对你有帮助,没有问题的话,请采纳。

相关阅读

  • java怎么创建新文件到webroot文件下

  • 乔山办公网excel表格制作
  • 放在webContent或者叫webRoot下,部署的时候这个文件夹会放到服务器上如果用工具,那就鼠标来点到webroot,右键,新建文件夹即可源,如果使用程序,则为String savePath = this.getServletContext
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网