我的上传文件到了tomcat下。我知道肯定是上传路径不对,不知道在哪里改。希望有人能帮忙指点下。
代码如下:
InputStream is = new FileInputStream(myFile);//获取上传文件
String path = ServletActionContext.getRequest().getRealPath("/file");
String tempname = path+"\\"myFileFileName;
OutputStream os = new FileOutputStream(tempname);//IO操作文件复制到指定目录
while((len = is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
代码如下:
InputStream is = new FileInputStream(myFile);//获取上传文件
String path = ServletActionContext.getRequest().getRealPath("/file");
String tempname = path+"\\"myFileFileName;
OutputStream os = new FileOutputStream(tempname);//IO操作文件复制到指定目录
while((len = is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
解决方案 »
- 稍大点的程序中,如何在模块间传递消息
- 急救
- 在SPRING2.5与HIBERNATE3.0结合的项目中 spring是怎么管理session的?
- 开发jms出现java.lang.NoClassDefFoundError: weblogic/transaction/internal/TransactionHelperImpl异常
- 菜鸟想要个b/s的聊天室代码
- ireport菜单栏中字体列表中的中文字体名怎么都是乱码,
- Hibernate中 方法链的调用顺序
- <html:form>中action属性的 test.jsp和test.do的问题
- o.s.web.context.ContextLoader [ContextLoader.java : 308] Context initialization
- 高分问问题,配完struts出现以下问题,在线等
- struts2的Action如何设置pager.offset方法?
- 找一hibernate 资料
首先要把 /Tomcat/webapps/ 这个目录清空,删掉该目录的所有内容,
然后把 /Tomcat/conf/web.xml 的监听器改为 true<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value><!--把false改为true-->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>在 /Tomcat/conf/server.xml 中配置工程的虚拟路径<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!----><Context path="/deco" docBase="E:\lft\deco\WebRoot"></Context>
<!--<Context path="/ser" docBase="D:\project\myProject\editor\WebRoot"></Context>-->
<!--这里可以配置多个工程的虚拟路径, 不想运行的工程就注释掉吧-->
</Host>
这样配置后,
String path = ServletActionContext.getRequest().getRealPath("/");
/**
得到的就是你工程的WebRoot根目录, 文件上传时就传到了工程目录下,
request.getRealPath(String arg) 这个方法已经过时,
还是用 request.getSession().getServletContext().getRealPath(String arg) 这个方法吧
String tempname = path+"\\"myFileFileName; 这里需要注意
不要用双斜杠"\\",尽量用反斜杠"/"
因为双斜杠在linux系统中不能被识别
*/
在调用的时候只要获得上下文路径然后拼接文件名就可以了。