当前位置:首页 » 文件管理 » java图片上传控件

java图片上传控件

发布时间: 2023-03-13 02:33:52

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);
}

⑵ javaWEB项目多个文件(如图片)上传,如何实现,有没有什么控件

用Apache commons-fileupload 几行代码就可以了。

⑶ 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应该也可以,只是我没有试过,恩,你理解的很对

⑷ 推荐一款适合javaEE项目的头像图片上传控件吧看清要求再回答

这种东西一般用jquery就可以做了,你自己可以看看jquery的用法,既然你在做javaEE了,应该有一定的自学能力。给你一个地址,你自己参考,我也没试过,不过应该没问题
http://www.3lian.com/e/2013/11-13/107280.html

⑸ 用java完成图片多张批量上传的功能,还有就是后台的应该怎么处理上传的照片。

环境准备

1. 下载并安装Tomcat(已经有很多关于Tomcat安装以及使用的文章,在这里不再介绍);

2. 下载File upload的jar包commons-fileupload-1.0-beta-1.jar,并将该文件拷贝到{$TOMCAT}/common/lib目录下(其中{$TOMCAT}为Tomcat的安装目录);

3. 由于Fileupload子项目同时要用到另外一个项目commons-Beanutils,所以必须下载Beanutils,并将解压后的文件commons-beanutils.jar拷贝到{$TOMCAT}/common/lib目录下。

开发文件上传页面

文件上传的界面如图1所示。为了增加效率我们设计了三个文件域,同时上传三个文件。
图1 文件上传界面

页面的HTML代码如下:

<html>
<head>
<title>文件上传演示</title>
</head>
<body bgcolor=“#FFFFFF”text=“#000000” leftmargin=“0”topmargin=“40”marginwidth=“0” marginheight=“0”>
<center>
<h1>文件上传演示</h1>
<form name=“uploadform”method=“POST” action=“save.jsp”ENCTYPE=“multipart/form-data”>
<table border=“1”width=“450”cellpadding=“4” cellspacing=“2”bordercolor=“#9BD7FF”>
<tr><td width=“100%”colspan=“2”>
文件1:<input name=“file1”size=“40”type=“file”>
</td></tr>
<tr><td width=“100%”colspan=“2”>
文件2:<input name=“file2”size=“40”type=“file”>
</td></tr>
<tr><td width=“100%”colspan=“2”>
文件3:<input name=“file3”size=“40”type=“file”>
</td></tr>
</table>
<br/><br/>
<table>
<tr><td align=“center”><input name=“upload” type=“submit”value=“开始上传”/></td></tr>
</table>
</form>
</center>
</body>
</html>

代码中要特别注意的是黑体处。必须保证表单的ENCTYPE属性值为multipart/form-data,这样浏览器才能正确执行上传文件的操作。

处理上传文件信息

由于本文主要是讲述如何使用Commons-fileupload,所以为了便于修改、调试,上传文件的保存使用一个JSP文件来进行处理。我们将浏览器上传来的所有文件保存在一个指定目录下并在页面上显示所有上传文件的详细信息。保存页面处理结果见图2所示。
图2 保存页面

下面来看看save.jsp的代码:

<%
/**
* 演示文件上传的处理
* @author <a href=“mailto:[email protected]”>Winter Lau</a>
* @version $Id: save.jsp,v 1.00 2003/03/01 10:10:15
*/
%>
<%@ page language=“java”contentType=“text/html;charset=GBK”%>
<%@ page import=“java.util.*”%>
<%@ page import=“org.apache.commons.fileupload.*”%>
<html>
<head>
<title>保存上传文件</title>
</head>
<%
String msg = “”;
FileUpload fu = new FileUpload();
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(“C:\\TEMP”);
//开始读取上传信息
List fileItems = fu.parseRequest(request);
%>
<body bgcolor=“#FFFFFF”text=“#000000” leftmargin=“0”topmargin=“40”marginwidth=“0” marginheight=“0”>
<font size=“6”color=“blue”>文件列表:</font>
<center>
<table cellpadding=0 cellspacing=1 border=1 width=“100%”>
<tr>
<td bgcolor=“#008080”>文件名</td>
<td bgcolor=“#008080”>大小</td>
</tr>
<%
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName();
long size = item.getSize();
if((name==null||name.equals(“”)) && size==0)
continue;
%>
<tr>
<td><%=item.getName()%></td>
<td><%=item.getSize()%></td>
</tr>
<%
//保存上传的文件到指定的目录
name = name.replace(‘:’,‘_’);
name = name.replace(‘\\’,‘_’);
item.write(“F:\\”+ name);
}
}
%>
</table>

<br/><br/>
<a href=“upload.html”>返回上传页面</a>
</center>
</body>
</html>

在这个文件中需要注意的是FileUpload对象的一些参数值的意义,如下面代码所示的三个参数sizeMax、sizeThreshold、repositoryPath:

FileUpload fu = new FileUpload();
// 设置允许用户上传文件大小,单位:字节
fu.setSizeMax(10000000);
// maximum size that will be stored in memory?
// 设置最多只允许在内存中存储的数据,单位:字节
fu.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
fu.setRepositoryPath(“C:\\TEMP”);

这3个参数的意义分别为:

SizeMax 用来设置上传文件大小的最大值,一旦用户上传的文件大小超过该值时将会抛出一个FileUploadException异常,提示文件太大;

SizeThreshold 设置内存中缓冲区的大小,一旦文件的大小超过该值的时候,程序会自动将其它数据存放在repositoryPath指定的目录下作为缓冲。合理设置该参数的值可以保证服务器稳定高效的运行;

RepositoryPath 指定缓冲区目录。

使用注意事项
从实际应用的结果来看该模块能够稳定高效的工作。其中参数SizeThreshold的值至关重要,设置太大会占用过多的内存,设置太小会频繁使用硬盘作为缓冲以致牺牲性能。因此,设置该值时要根据用户上传文件大小分布情况来设定。例如大部分文件大小集中在100KB左右,则可以使用100KB作为该参数的值,当然了再大就不合适了。使用commons-fileupload来处理HTTP文件上传的功能模块很小,但是值得研究的东西很多。

⑹ Java在jsp中 如何上传图片 在上传时可以取到图片大小并修改

我有个想法。放一个图片控件。加载之后。获取这个图片控件的宽度和高度 就是图片的宽度很高度。 用offset来获龋 当获取宽度之后 隐藏这个控件。如果需要的话显示也行

⑺ 跪求一个java 上传图片到服务器的工具类

/**
*文件上传处理主程序。
*
*@returnint操作结果0文件操作成功;1request对象不存在。2没有设定文件保存路径或者文件保存路径不正确;3
*没有设定正确的enctype;4文件操作异常。
*/
publicMap<String,String>fileupload_java(HttpServletRequestrequest,Stringuploadpath){
Map<String,String>param=newHashMap<String,String>();

try{
//参数或者文件名
Stringname=null;
//参数的value
Stringvalue=null;
//读取的流是否为文件的标志位
booleanfileFlag=false;
//要存储的文件。
FiletmpFile=null;
//上传的文件的名字
StringfName=null;
FileOutputStreambaos=null;
BufferedOutputStreambos=null;
intrtnPos=0;
byte[]buffs=newbyte[BUFSIZE*8];

//取得ContentType
StringcontentType=request.getContentType();
intindex=contentType.indexOf("boundary=");
Stringboundary="--"+contentType.substring(index+9);
StringendBoundary=boundary+"--";

//从request对象中取得流。
ServletInputStreamsis=request.getInputStream();
//读取1行
while((rtnPos=sis.readLine(buffs,0,buffs.length))!=-1){

StringstrBuff=newString(buffs,0,rtnPos);
if(strBuff.startsWith(boundary)){
if(name!=null&&name.trim().length()>0){
if(fileFlag){
bos.flush();
baos.close();
bos.close();
baos=null;
bos=null;
param.put(name,tmpFile.getAbsolutePath());
}else{
StringparamValue=param.get(name);
paramValue+=","+value;
param.put(name,paramValue);
}
}
name=newString();
value=newString();
fileFlag=false;
fName=newString();
rtnPos=sis.readLine(buffs,0,buffs.length);
if(rtnPos!=-1){
strBuff=newString(buffs,0,rtnPos);
if(strBuff.toLowerCase().startsWith("content-disposition:form-data;")){
intnIndex=strBuff.toLowerCase().indexOf("name="");
intnLastIndex=strBuff.toLowerCase().indexOf(""",nIndex+6);
name=strBuff.substring(nIndex+6,nLastIndex);
}
intfIndex=strBuff.toLowerCase().indexOf("filename="");
if(fIndex!=-1){
fileFlag=true;
intfLastIndex=strBuff.toLowerCase().indexOf(""",fIndex+10);
//fName=strBuff.substring(fIndex+10,fLastIndex);
fName=newString(strBuff.substring(fIndex+10,fLastIndex).getBytes(),"gbk");
fName=FileL.getFileNameWithoutSeprater(fName);
if(fName==null||fName.trim().length()==0){
fileFlag=false;
sis.readLine(buffs,0,buffs.length);
sis.readLine(buffs,0,buffs.length);
sis.readLine(buffs,0,buffs.length);
continue;
}else{
fName=FileL.getFileNameTime(fName);
sis.readLine(buffs,0,buffs.length);
sis.readLine(buffs,0,buffs.length);
}
}
}
}elseif(strBuff.startsWith(endBoundary)){
if(name!=null&&name.trim().length()>0){
if(fileFlag){
bos.flush();
baos.close();
bos.close();
baos=null;
bos=null;
param.put(name,tmpFile.getAbsolutePath());
}else{
StringparamValue=param.get(name);
paramValue+=","+value;
param.put(name,paramValue);
}
}
}else{
if(fileFlag){
if(baos==null&&bos==null){
tmpFile=newFile(uploadpath+fName);
baos=newFileOutputStream(tmpFile);
bos=newBufferedOutputStream(baos);
}
bos.write(buffs,0,rtnPos);
baos.flush();
}else{
value=value+strBuff;
}
}
}
}catch(IOExceptione){
e.printStackTrace();
}
returnparam;
}

⑻ 用Java Web的jsp制作图片上传和显示如何实现

用jspSmartUpload组件来实现,用jsp+servlet在Servlet里实现的代码:

PrintWriter out = response.getWriter();
int count = 0;
// 实例化上传控件对象
SmartUpload su = new SmartUpload();
// 初始化操作
su.initialize(config, request, response);

// 设置上传文件最大字节数
su.setTotalMaxFileSize(100000);

//
try {
//禁止上传指定扩展名的文件
su.setDeniedFilesList("ext,bat,jsp");
} catch (SQLException e1) {
e1.printStackTrace();
}

try {
// 上传文件到服务器
su.upload();

File fileup = new File(request.getRealPath("upload"));
if(!fileup.exists()){
// 创建目录
fileup.mkdir();
}
// 处理多个文件的上传
for(int i = 0;i < su.getFiles().getCount();i++){
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if(!file.isMissing()){ // 如果文件有效
// 保存文件到指定上传目录
file.saveAs("/upload/new."+file.getFileExt(), su.SAVE_VIRTUAL);
count = su.save("/upload");
}
}

} catch (SmartUploadException e) {

e.printStackTrace();
}
out.println(count +"file(s) uploaded");

如果你对这个上传组件不了解,最好是先去查查用法。。。

⑼ java使用uploadify上传图片,打开图片后不添加到上传队列

formdata这个参数没设置啊,你要调用哪个service,哪个方法,什么参数都没,,,

热点内容
内置存储卡可以拆吗 发布:2025-05-18 04:16:35 浏览:333
编译原理课时设置 发布:2025-05-18 04:13:28 浏览:377
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:610
java用什么软件写 发布:2025-05-18 03:56:19 浏览:31
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:107
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:942
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:739
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:802
网卡访问 发布:2025-05-18 03:35:04 浏览:510
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:371