Struts2 我文件上传可以了,但上传不到特定的文件夹里面,在tomcat里面。好郁闷啊 应该怎样办啊?
getset方法忽略:publicStringupload(){StringnewPicName=newDate().getTime()+picFileName.subs...
get set 方法忽略:
public String upload()
{
String newPicName=new Date().getTime()+picFileName.substring(picFileName.indexOf("."));
String dstPath = ServletActionContext.getServletContext().getRealPath("upload")+ "\\" + newPicName;
File dstFile = new File(dstPath);
copy(this.getPic(),dstFile);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("newPicName", newPicName);
request.put("oldPicName", picFileName);
request.put("fujianPath", "/upload"+ "/" +newPicName);
return ActionSupport.SUCCESS;
}
private static void copy(File src, File dst)
{
InputStream in = null;
OutputStream out = null;
try
{
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (null != in)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (null != out)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} 展开
public String upload()
{
String newPicName=new Date().getTime()+picFileName.substring(picFileName.indexOf("."));
String dstPath = ServletActionContext.getServletContext().getRealPath("upload")+ "\\" + newPicName;
File dstFile = new File(dstPath);
copy(this.getPic(),dstFile);
Map request=(Map)ServletActionContext.getContext().get("request");
request.put("newPicName", newPicName);
request.put("oldPicName", picFileName);
request.put("fujianPath", "/upload"+ "/" +newPicName);
return ActionSupport.SUCCESS;
}
private static void copy(File src, File dst)
{
InputStream in = null;
OutputStream out = null;
try
{
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (null != in)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (null != out)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} 展开
2个回答
展开全部
String dstPath = ServletActionContext.getServletContext().getRealPath("upload")+ "\\" + newPicName;
这个dstPath就是上传的目录了, ServletActionContext.getServletContext().getRealPath("upload")这句说明上传到tomcat下的应用下面的upload目录。
比如如果要传到c盘下的upload目录下 则改为String dstPath = "c:"+ "\\upload" + newPicName;
这个dstPath就是上传的目录了, ServletActionContext.getServletContext().getRealPath("upload")这句说明上传到tomcat下的应用下面的upload目录。
比如如果要传到c盘下的upload目录下 则改为String dstPath = "c:"+ "\\upload" + newPicName;
展开全部
struts2 可以配置上传文件的临时目录,在你的struts.xml或struts.properties中指定即可:
struts.multipart.saveDir=...
//文件上传到服务器的临时目录,如果不配置将采用servle的t默认临时目录
可以参考:
http://blog.csdn.net/littleatp2008/article/details/6758192
struts.multipart.saveDir=...
//文件上传到服务器的临时目录,如果不配置将采用servle的t默认临时目录
可以参考:
http://blog.csdn.net/littleatp2008/article/details/6758192
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询