当前位置:首页 » 文件管理 » javaservlet接收图片上传

javaservlet接收图片上传

发布时间: 2022-10-20 08:00:48

1. jsp+servlet 上传图片并显示出来

其实你这个挡也显示图片其实很简单的,
你的需求无非是两个
1.servlet上传文件(图片)
2.点击 浏览 图标,然后选择图片文件,然后就可以在页面中的某个地方看到图片

是这两个需求么?
首先说第二个吧。
你上传图片之后,就马上触发js函数,内容为
var PicPath = document.getElementById("yourfile").value;
document.getElementById("yourDiv").innerHTML="<IMG src="+PicPath+"/>";
OK了

第一个嘛就无所谓说了,不过我还是贴一个代码吧,
public void upLoadFile(HttpServletRequest request, HttpServletResponse response) {
PrintWriter out = null;
response.setCharacterEncoding("UTF-8");
//实例化文件工厂
FileItemFactory factory = new DiskFileItemFactory();
//配置上传组件ServletFileUpload
ServletFileUpload upload = new ServletFileUpload(factory);
try {
out = response.getWriter();
//从request得到所有上传域的列表
List<FileItem> list = upload.parseRequest(request);

for (FileItem item : list) {
//isFormField判断一个item类对象封装的是一个普通的表单字段还是文件表单字段。
// 如果item是文件域,则做出如下处理:
if (!item.isFormField()) {

//上传文件域的Name
String fileName = item.getName();

//截取扩展名
int idx = fileName.lastIndexOf(".");
String extension = fileName.substring(idx);

//获取文件名
String name = new Date().getTime() + extension;

//得到文件夹的物理路径
String path = this.getServletContext().getRealPath("\\upload");

//创建一个File
File file = new File(path + "\\" + name);
FileOutputStream o = new FileOutputStream(file);
InputStream in = item.getInputStream();
try {
LoadProcessServlet.process = 0;
LoadProcessServlet.total = 100;
LoadProcessServlet.isEnd = false;
LoadProcessServlet.total = item.getSize();
byte b[] = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
LoadProcessServlet.process+=n;
o.write(b, 0, n);
System.out.println("实际:"+LoadProcessServlet.process);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
LoadProcessServlet.isEnd = true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}

}

2. java接收from表单提交的多图片,怎么点击提交的时候一起上传

首先,文本类的可以放在request中通过request.getAttribute(name)获取。图片你在前端放地址,后端也是像前面通过request.getAttribute(name)获取后存入数据库。这是jsp+servlet的做法。jsp有九大内置对象用于传递数据。而你如果用spring+springmvc的话是通过参数绑定来传递数据的。详细的你可以了解框架文档。建议你选择一种框架可以便捷开发。jsp+servlet是比较原始的处理方式。

3. java的servlet如何将图片传到jsp页面上

servlet里面有个setAttribute,你用这个给图片的路径放进去,在JSP页面a标签里用EL表达式显示出来。

4. java实现图片上传至服务器并显示,如何做希望要具体的代码实现

很简单。
可以手写IO读写(有点麻烦)。
怕麻烦的话使用FileUpload组件 在servlet里doPost嵌入一下代码
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();

//设置保存上传文件的目录
String uploadDir =getServletContext().getRealPath("/up");
System.out.println(uploadDir);
if (uploadDir == null)
{
out.println("无法访问存储目录!");
return;
}
//根据路径创建一个文件
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()){
if(!fUploadDir.mkdir())//如果UP目录不存在 创建一个 不能创建输出...
{
out.println("无法创建存储目录!");
return;
}
}

if (!DiskFileUpload.isMultipartContent(request))
{
out.println("只能处理multipart/form-data类型的数据!");
return ;
}

DiskFileUpload fu = new DiskFileUpload();
//最多上传200M数据
fu.setSizeMax(1024 * 1024 * 200);
//超过1M的字段数据采用临时文件缓存
fu.setSizeThreshold(1024 * 1024);
//采用默认的临时文件存储位置
//fu.setRepositoryPath(...);
//设置上传的普通字段的名称和文件字段的文件名所采用的字符集编码
fu.setHeaderEncoding("gb2312");

//得到所有表单字段对象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);//解析request对象中上传的文件

}
catch (FileUploadException e)
{
out.println("解析数据时出现如下问题:");
e.printStackTrace(out);
return;
}

//处理每个表单字段
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField()){
String content = fi.getString("GB2312");
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}else{
try
{
String pathSrc = fi.getName();
if(pathSrc.trim().equals("")){
continue;
}
int start = pathSrc.lastIndexOf('\\');
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName);

fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}catch (Exception e){
out.println("存储文件时出现如下问题:");
e.printStackTrace(out);
return;
}
finally //总是立即删除保存表单字段内容的临时文件
{
fi.delete();
}

}
}
注意 JSP页面的form要加enctype="multipart/form-data" 属性, 提交的时候要向服务器说明一下 此页面包含文件。

如果 还是麻烦,干脆使用Struts 的上传组件 他对FileUpload又做了封装,使用起来更傻瓜化,很容易掌握。

-----------------------------
以上回答,如有不明白可以联系我。

5. java怎样上传图片(写个例子谢谢);

用struts2框架。。。马上OK。。。
在响应的ACTION中,只需要定义一个File的变量即可

6. java写服务端,用servlet接收客户端上传的音频,视频,图片,写到本地硬盘,唯独图片打不开

你看看,你设定的输出流后面的参数是不是吧图片的后缀给忘记了?往本地硬盘上写的时候,读取过来后,写出去之前是可以更改名称 和后缀名的

7. java jsp 怎么上传图片到servlet

jsp:
<form action="up.do" method="post" enctype="multipart/form-data" id="upform"> 文件:<input type="file" name="file" id="file"/> <input type="button" id="btn" value="上传"/> </form>

SERVLET:

FileItemFactory fileItemFactory = new DiskFileItemFactory(); ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory); //设置 中文 编码集 fileUpload.setHeaderEncoding("UTF-8"); try { List<FileItem> fileItems= fileUpload.parseRequest(request); for (FileItem fi : fileItems) { if(fi.isFormField()){ } } } for (FileItem fileItem : fileItems) { if(fileItem.isFormField()){ }else{ //文件名 String filename = fileItem.getName(); //文件名处理 if(filename.indexOf('\\') != -1){ filename = filename.substring(filename.lastIndexOf('\\')+1); } System.out.println(filename); if(filename == null || filename.equals("")){ request.setAttribute("msg", "没有指定上传文件"); request.getRequestDispatcher("index.jsp").forward(request, response); //验证码错误 就退出 return; } //path 生成文件夹 String fpath = "F:\\\\中软高科\\\\小组\\\\5组日报"+UpFileUtil.getMonthadnDate(); File file = new File(fpath); if(!file.exists()){ file.mkdir(); } //文件全路径 fpath = fpath+"\\\\"+filename;// System.out.println(filename+"--->"+fpath+"--->"+UpFileUtil.getTime()); //写出文件到 服务器磁盘 InputStream in = fileItem.getInputStream();//request.getInputStream(); OutputStream out = new FileOutputStream(fpath); byte[] buff = new byte[1024*10]; while(in.read(buff) != -1){ out.write(buff); } out.flush(); out.close(); in.close(); //保存文件信息到数据库 new UpFileDao().updFile("{call file_add(?,?,?)}",new String[]{filename,fpath,UpFileUtil.getTime()}); filename = null; fpath = null; file = null; request.setAttribute("msg", "上传成功"); request.getRequestDispatcher("index.jsp").forward(request, response); } } } catch (Exception e) { e.printStackTrace(); }

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

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

9. java servlet如何接收其他程序传来的图片和参数!

在jsp页面中将图片转换的二进制数组,Servlet中的用request.getpari........(变量明)来接受即可!不明白再Hi我。我都在线的!

10. java:servlet接收图片,并把它保存到数据库中

这种代码网上不是一大片吗

publicbooleanstoreImage(Filefile){
try{
//打开文件
FileInputStreamfin=newFileInputStream(file);
//建一个缓冲保存数据
ByteBuffernbf=ByteBuffer.allocate((int)file.length());
byte[]array=newbyte[1024];
intoffset=0,length=0;
//读存数据
while((length=fin.read(array))>0){
if(length!=1024)nbf.put(array,0,length);
elsenbf.put(array);
offset+=length;
}
//关闭文件
fin.close();
//新建一个数组保存要写的内容
byte[]content=nbf.array();
Stringsql="insertintoimages(bin_data)values(?)";
PreparedStatementpstmt=conn.prepareStatement(sql);
pstmt.setBytes(1,content);
pstmt.execute();
pstmt.close();
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
热点内容
直出服务器怎么样 发布:2024-10-07 15:41:36 浏览:476
比亚迪唐dmi哪个配置性价比 发布:2024-10-07 15:19:28 浏览:901
编译器按变量 发布:2024-10-07 15:07:03 浏览:773
怎么忘记电脑wifi密码怎么办 发布:2024-10-07 15:02:18 浏览:424
安卓开发java开发 发布:2024-10-07 15:01:29 浏览:94
工业级安卓主板价格怎么样 发布:2024-10-07 14:07:57 浏览:627
编程先乘除 发布:2024-10-07 13:58:45 浏览:269
编译内核时发生循环编译 发布:2024-10-07 13:58:43 浏览:495
当下笔记本电脑什么配置好 发布:2024-10-07 12:57:33 浏览:471
安卓倒车轨迹怎么调 发布:2024-10-07 12:54:47 浏览:916