首页服务器java上传图片到服务器(java 中如何向服务器上传图片)

java上传图片到服务器(java 中如何向服务器上传图片)

编程之家2023-10-2089次浏览

今天给各位分享java上传图片到服务器的知识,其中也会对java 中如何向服务器上传图片进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

java上传图片到服务器(java 中如何向服务器上传图片)

java上传图片到远程服务器上,怎么解决呢

需要这样的一个包 jcifs-1.1.11

public static void forcdt(String dir){

InputStream in= null;

OutputStream out= null;

File localFile= new File(dir);

try{

java上传图片到服务器(java 中如何向服务器上传图片)

//创建file类传入本地文件路径

//获得本地文件的名字

String fileName= localFile.getName();

//将本地文件的名字和远程目录的名字拼接在一起

//确保上传后的文件于本地文件名字相同

SmbFile remoteFile= new SmbFile("smb://administrator:admin@10.0.0.1/e$/aa/");

java上传图片到服务器(java 中如何向服务器上传图片)

//创建读取缓冲流把本地的文件与程序连接在一起

in= new BufferedInputStream(new FileInputStream(localFile));

//创建一个写出缓冲流(注意jcifs-1.3.15.jar包类名为Smb开头的类为控制远程共享计算机"io"包)

//将远程的文件路径传入SmbFileOutputStream中并用缓冲流套接

out= new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName));

//创建中转字节数组

byte[] buffer= new byte[1024];

while(in.read(buffer)!=-1){//in对象的read方法返回-1为文件以读取完毕

out.write(buffer);

buffer= new byte[1024];

}

}catch(Exception e){

e.printStackTrace();

}finally{

try{

//注意用完操作io对象的方法后关闭这些资源,走则造成文件上传失败等问题。!

out.close();

in.close();

}catch(Exception e){

e.printStackTrace();}

}

}

java实现图片上传至服务器并显示,如何做

给你段代码,是用来在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这个图片

你上面的问题:

1.我觉得你的第二个办法是对的,我们也是这样做的,需要的是把数据库的记录id号传进servlet,然后读取这条记录中的路径信息,生成流以后返回就是了

关于上传文件的问题,我记得java中应该专门有个负责文件上传的类,你调用就行了,上传后存储在指定的目录里,以实体文件的形式存放

你可以参考这个:

http://blog.csdn.net/arielxp/archive/2004/09/28/119592.aspx

回复:

1.是的,在response中写入流就行了

2.是发到servlet中的,我们一般都是写成servlet,短小精悍,使用起来方便,struts应该也可以,只是我没有试过,恩,你理解的很对

java 中如何向服务器上传图片

我们使用一些已有的组件帮助我们实现这种上传功能。

常用的上传组件:

Apache的 Commons FileUpload

JavaZoom的UploadBean

jspSmartUpload

以下,以FileUpload为例讲解

1、在jsp端

<form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data">

要注意enctype="multipart/form-data"

然后只需要放置一个file控件,并执行submit操作即可

<input name="file" type="file" size="20">

<input type="submit" name="submit" value="提交">

2、web端

核心代码如下:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

request.setCharacterEncoding("UTF-8");

DiskFileItemFactory factory= new DiskFileItemFactory();

ServletFileUpload upload= new ServletFileUpload(factory);

try{

List items= upload.parseRequest(request);

Iterator itr= items.iterator();

while(itr.hasNext()){

FileItem item=(FileItem) itr.next();

if(item.isFormField()){

System.out.println("表单参数名:"+ item.getFieldName()+",表单参数值:"+ item.getString("UTF-8"));

} else{

if(item.getName()!= null&&!item.getName().equals("")){

System.out.println("上传文件的大小:"+ item.getSize());

System.out.println("上传文件的类型:"+ item.getContentType());

System.out.println("上传文件的名称:"+ item.getName());

File tempFile= new File(item.getName());

File file= new File(sc.getRealPath("/")+ savePath, tempFile.getName());

item.write(file);

request.setAttribute("upload.message","上传文件成功!");

}else{

request.setAttribute("upload.message","没有选择上传文件!");

}

}

}

}catch(FileUploadException e){

e.printStackTrace();

} catch(Exception e){

e.printStackTrace();

request.setAttribute("upload.message","上传文件失败!");

}

request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);

}

java上传图片到服务器和java 中如何向服务器上传图片的问题分享结束啦,以上的文章解决了您的问题吗?欢迎您下次再来哦!

企业邮箱域名注册,企业邮箱怎么注册申请域名安全检测 如何检测网址是否安全