导航:首页 > 编程语言 > java本地图片上传

java本地图片上传

发布时间:2023-01-30 12:05:06

java 如何只通过后台把本地的图片上传的服务器上去

这里你弄错了一个问题;x0dx0a你的程序是要传递图片的二进制数据.x0dx0a而不是传递路径,然后再到服务器读取文件数据(你的服务器有这个文件?)x0dx0a只有当你的服务器下有这个文件了,你传递一个路径,读取是可以的.x0dx0a//---x0dx0a关于如何上传文件, 自己google一下,很多教程

② 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 如何只通过后台把本地的图片上传的服务器上去

importjava.io.*;
publicclassCopyIMG{
publicstaticvoidmain(String[]args)throwsException{
Filefile=newFile("C:\xx.jpg");
if(!file.exists())
thrownewRuntimeException("文件不存在..");
FileInputStreamfis=newFileInputStream(file);
byte[]b=newbyte[1024];
intlen=0;
FileOutputStreamfos=newFileOutStream("要保存的服务器路径");
while((len=is.read(b))!=-1){
fos.write(b,0,len);
}
fos.close();
fis.close();
}
}

④ 怎么用Java实现图片上传

下面这是servlet的内容:
package demo;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class DemoServlet extends HttpServlet {

private static final String UPLOAD_DIRECTORY = "upload";
private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; // 3MB
private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; // 50MB

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload sfu=new ServletFileUpload(factory);
sfu.setHeaderEncoding("UTF-8");
sfu.setProgressListener(new ProgressListener() {

public void update(long pBytesRead, long pContentLength, int pItems) {
System.out.println("文件大小为:"+pContentLength+",当前已处理:"+pBytesRead);

}
});
//判断提交上来的数据是否是上传表单的数据
if(!ServletFileUpload.isMultipartContent(request)){
PrintWriter writer= response.getWriter();
writer.println("Error:表单必须包含 enctype=multipart/form-data");
writer.flush();
return;
}
factory.setSizeThreshold(MEMORY_THRESHOLD);
//设置临时储存目录
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
//设置最大文件上传值
sfu.setFileSizeMax(MAX_FILE_SIZE);
//设置最大请求值(包含文件和表单数据)
sfu.setSizeMax(MAX_REQUEST_SIZE);
String uploadpath=getServletContext().getRealPath("./")+ File.separator+UPLOAD_DIRECTORY;
File file=new File(uploadpath);
if(!file.exists()){
file.mkdir();
}

try {
List<FileItem> formItems = sfu.parseRequest(request);
if(formItems!=null&&formItems.size()>0){
for(FileItem item:formItems){
if(!item.isFormField()){
String fileName=new File(item.getName()).getName();
String filePath=uploadpath+File.separator+fileName;
File storeFile=new File(filePath);
System.out.println(filePath);
item.write(storeFile);
request.setAttribute("message", "文件上传成功!");
}
}
}
} catch (Exception e) {
request.setAttribute("message", "错误信息:"+e.getMessage());
}
getServletContext().getRequestDispatcher("/demo.jsp").forward(request, response);
}

}

下面是jsp的内容,jsp放到webapp下,如果想放到WEB-INF下就把servlet里转发的路径改一下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="demo.do" enctype="multipart/form-data" method="post">
<input type="file" name="file1" />
<%
String message = (String) request.getAttribute("message");
%>
<%=message%>
<input type="submit" value="提交"/>
</form>
</body>
</html>
这段代码可以实现普通的文件上传,有大小限制,上传普通的图片肯定没问题,别的一些小的文件也能传

⑤ java 图片上传

//1.初始化smartupload对象
SmartUpload su=new SmartUpload();
su.initialize(pageContext);

//2.定义上传文件类型
su.setAllowedFilesList("gif,jpg,doc,txt");
//3.不允许上传类型
su.setDeniedFilesList("jsp,asp,html,exe,bat");
//4.设置字符编码、
su.setCharset("UTF-8");
//5.设置的单个上传最大限制
su.setMaxFileSize(5*1024*1024);
//6.总共上传限制
su.setTotalMaxFileSize(20*1024*1024);
//7.上传
su.upload();
//su.getFiles().getCount() 获取上传数
File file=su.getFiles().getFile(0);
String filename=file.getFileName();
System.out.print(filename);
String filepath="upload\\";
filepath+=file.getFileName();
file.saveAs(filepath,SmartUpload.SAVE_VIRTUAL);

⑥ Java图片上传处理

upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" %>
<html>
<head>
<title>上传文件 </title>

</head>
<body>
<form id="form1" name="msform" method="post" action="do_upload.jsp" enctype="multipart/form-data" onSubmit="return Check_Found(this);" target="iframe1">
<table width="50%" border="1" align="center">
<tr>
<td align="center"><input type="text" name="name" id="text1"></td>
</tr>
<tr>
<td align="center">产品说明:
<input type="file" name="file2" value=""/>
<iframe name="iframe1" style="display:none"> </iframe>
<input type="submit" name="Submit" value="上传图片" />
</td>
</tr>
</table>
</form>
</body>
</html>

do_upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" %>
<html>
<head>
<title>文件上传处理页面 </title>

</head>
<body>
<%
SmartUpload su=new SmartUpload();
su.initialize(pageContext);
su.upload();
String name;
int count=su.save("/upload",su.SAVE_VIRTUAL);
out.println(count+"个文件上传成功! <br>");
for(int i=0;i <su.getFiles().getCount();i++)
{
com.jspsmart.upload.File file=su.getFiles().getFile(i);
if(file.isMissing()) continue;
String files=file.getFileName();
out.print("<script>window.parent.document.text1.value='../upload/"+file.getFileName()+"';</script>");
out.print("<script>alert('上传成功!');</script>");
response.setHeader("Refresh","0;URL=addnewproce.jsp");
}
%>
</body>
</html>

还需要组件才行

⑦ java怎么上传图片

  1. 使用Commons-FileUpload 上传文件

  2. Struts 的文件上传

  3. jspSmartUpload上传文件

⑧ 请问用Java 如何实现图片上传功能

自己写程序来上传字节流文件很难的,用SmartUpload.jar包吧,专门用于JSP上传下载的,唯一缺点就是中文支持不太好,不过你可以改一下原程序的字符集就行了。上网搜,没有找我!我给你发

⑨ java 如何只通过后台把本地的图片上传的服务器上去

首先 你是客户端。服务器端必须有这个功能你才能上传。一般的就是做一个页面有上传的功能。或者你可以在客户端写个上传文件的程序,请求服务器的上传功能。这样不用打开页面。

阅读全文

与java本地图片上传相关的资料

热点内容
隐身侠加密宝手机版 浏览:135
农行app怎么办理签约手续 浏览:185
汽车压力解压神器 浏览:307
家用冰箱压缩机有风扇吗 浏览:647
安卓qq年龄怎么看 浏览:839
屏幕跳屏乱点app怎么解决 浏览:414
turbo加速器android 浏览:432
洪尚秀的电影哪里哪个app能看 浏览:151
百度网站加密视频怎么下载 浏览:135
台州ug产品编程培训班 浏览:866
Javalinux字体 浏览:520
77万年会程序员补贴 浏览:610
灭火是由近及源码 浏览:158
资料服务器地址 浏览:8
网页怎么放在服务器商 浏览:995
服务器出差错是什么原因 浏览:595
如何修改通达信主图指标源码 浏览:352
联想硬盘加密教程 浏览:65
无锡江苏服务器服务商云服务器 浏览:657
服务器systembooting什么意思 浏览:994