通过JSP怎样上传图片到服务器?

Spring89 2009-11-26 09:52:38
我要通过JSP把用户上传的图片保存在服务器的磁盘上,目前我上传图片的位置是“D:\\Kevin\\Projects\\dev_server\\51pharm_cn_attach\\pics\\prd_images”,这个是上传图片保存在我本地的地址,现在我想把上传的图片存放到服务器上这个路径下,服务器的IP地址:192.168.0.140,以及当用户上传图片成功后,显示刚上传的图片。请哪位大哥帮帮忙,怎样把图片存到服务器上?怎样从服务器上取图片?急急急......
...全文
602 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hequhecong126317 2010-03-23
  • 打赏
  • 举报
回复
顶一下,学习了
hyf0541210 2009-11-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hysondn 的回复:]
给你段代码,是用来在ie上显示图片的(servlet):

public void doGet(HttpServletRequest request, HttpServletResponse response)
  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这个图片
[/Quote]

帮忙顶一下
hysondn 2009-11-26
  • 打赏
  • 举报
回复
给你段代码,是用来在ie上显示图片的(servlet):

public void doGet(HttpServletRequest request, HttpServletResponse response)
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这个图片

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧