我要通过JSP把用户上传的图片保存在服务器的磁盘上,目前我上传图片的位置是“D:\\Kevin\\Projects\\dev_server\\51pharm_cn_attach\\pics\\prd_images”,这个是上传图片保存在我本地的地址,现在我想把上传的图片存放到服务器上这个路径下,服务器的IP地址:192.168.0.140,以及当用户上传图片成功后,显示刚上传的图片。请哪位大哥帮帮忙,怎样把图片存到服务器上?怎样从服务器上取图片?急急急......
调试欢乐多
throws ServletException, IOException {
String id = request.getParameter("id");
File file = new File(getServletContext().getRealPath("/")+"out"+"/"+id+".gif");
response.setCharacterEncoding("gb2312");
response.setContentType("doc");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("gb2312"),"iso8859-1"));
System.out.println(new String(file.getName().getBytes("gb2312"),"gb2312"));
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(file); byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b))!=-1)
{
output.write(b, 0, i);
}
output.write(b, 0, b.length);
output.flush();
response.flushBuffer();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
} }这个程序的功能是根据传入的文件名(id),来为浏览器返回图片流,显示在<img>标签里
标签的格式写成如下:
<img src="http://localhost:8080/app/preview?id=111 "/><br/>
显示的是111.gif这个图片