进google搜索smartupload,图片存到服务器,数据库存放图片地址。
解决方案 »
- 在MyEclipse里面调用Dll的方法。报Exception in thread "main" java.lang.UnsatisfiedLinkError
- struts2跳转基础问题
- containsAll()问题
- 讨论下java与.net的优缺点(晚9点半结贴)!
- 求高手帮忙 TOMCAT5.0 + MYSQL5 配置数据源 弄了几天了还不行,救命呀
- stateless session bean中的一个事务问题
- hibernate 非主键的关联 HQL查询取到的对象中的对象属性为空 求解
- 一个是用hibernate操作mysql的问题
- 关于j2ee事务处理的:使用bean管理还是容器管理好??
- EJBCA安装出错:ant install时出现Batch gerneration failed!
- 象我这样写 连接 和 关闭 数据有问题吗?
- 求救!struts-menu开发中的问题
<head>
<title>上传文件</title>
</head>
<body>
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<form action="upload.jsp" method="post" ENCTYPE="multipart/form-data">
<table bgColor="gray">
<tr>
<td colspan="2" align="center"><h2>输入的名称为上传后的名称</h2></td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td>输入文件的名称:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>选择要上传的图片:</td>
<td><input type="file" name="pic"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="提交"></td>
<td align="center"><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
<jsp:useBean id="smart" scope="page" class="org.lxh.smart.SmartUpload"/>
<%
//1.上传初始化
smart.initialize(pageContext);
//2.准备上传
smart.upload();
//3.保存上传的文件
//smart.save("/upload_test/upload");
String tail=smart.getFiles().getFile(0).getFileExt();
%>
<%
String name=smart.getRequest().getParameter("name");
%>
<%
//保存上传的文件
smart.getFiles().getFile(0).saveAs("/upload_test/upload/"+name+"."+tail);
%>
这是我写的,你可以参考一下
当然你还要下载一个jspsmartupload.jar,并把它放在WEB-INF目录下的lib文件夹下...
需要的话可以给我发邮件,我传给你...
[email protected]
已经把连接数据库操作数据库的代码封状好了,我们直接用就好了!
http://www.jzxue.com/Html/jsp/2007/9/492938H18.html
//上传图片
String aa=request.getParameter("goodsPic");
//取得图片文件
File fromfile=new File(aa);
int len=0;
BufferedInputStream fis=null;
BufferedOutputStream fos=null;
//获得系统时间作为图片的名字,防止重名
Date dd=new Date();
String goodspic=dd.getTime()+".gif";
File tofile=new File(request.getRealPath("/")+"/web/valuechain/image/"+goodspic);
try{
fis= new BufferedInputStream(new FileInputStream(fromfile));
fos= new BufferedOutputStream(new FileOutputStream(tofile));
int size=(int)fromfile.length();
byte[] buffer=new byte[size];
//写入文件
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流对象
try {
if (fos != null) {
fos.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
import java.io.IOException;
import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;
import java.sql.Statement;import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;public class uploadphoto extends HttpServlet {
private String uploadPath ; // 上传文件的目录
private String tempPath; // 临时文件目录
// public void setHeaderEncoding(String encoding)
{
// uploadPath = encoding;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=GB2312");
request.setCharacterEncoding("GB2312");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession();
uploadPath=request.getRealPath("/")+"upload/image"; //上传图片上相对路径
tempPath = request.getRealPath("/")+"upload/images/temp/";
java.net.URLEncoder.encode(uploadPath,"gb2312");
if(!new File(uploadPath).isDirectory())
new File(uploadPath).mkdirs();
if(!new File(tempPath).isDirectory())
new File(tempPath).mkdirs(); response.setCharacterEncoding("UTF-8");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判断是否有文件上传
if(isMultipart == true){
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sevletFileUpload = new ServletFileUpload(factory);
// 设置允许用户上传文件大小,单位:字节,这里设为2m
sevletFileUpload.setSizeMax(2 * 1024 * 1024);
// 设置最多只允许在内存中存储的数据,单位:字节
factory.setSizeThreshold(4096);
// 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
factory.setRepository(new File(tempPath));
// 开始读取上传信息
List fileItems = sevletFileUpload.parseRequest(request);//<FileItem>
// 依次处理每个上传的文件
Iterator iter = fileItems.iterator();
// 正则匹配,过滤路径取文件名
String regExp = ".+\\\\(.+)$";
// 过滤掉的文件类型
String[] errorType = { ".exe", ".com", ".cgi", ".asp" ,".rmvb"};
Pattern p = Pattern.compile(regExp);
int looptimes=0; //定义一个循环次数
while (iter.hasNext()) {
looptimes=looptimes+1;
FileItem item = (FileItem) iter.next();
// 忽略其他不是文件域的所有表单信息
if (!item.isFormField()) {
String name = item.getName(); //得到上传文件的
String fileName=name.substring(name.lastIndexOf("\\"));//取出文件名
String t_ext = fileName.substring(fileName.lastIndexOf(".") + 1);//取出文件的后缀名
long size = item.getSize();
if ((name == null || name.equals("")) && size == 0)
continue;
Matcher m = p.matcher(name);
boolean result = m.find();
if (result) {
for (int temp = 0; temp < errorType.length; temp++) {
if (m.group(1).endsWith(errorType[temp])) {
throw new IOException(name + ": 非法文件类型禁止上传");
}
}
long now = System.currentTimeMillis();//返回以毫秒为单位的当前时间
String prefix = String.valueOf(now);
String u_name = prefix + "." + t_ext;
try {
if (name != null) {
File savedFile = new File(uploadPath, u_name);
item.write(savedFile);
String photofile=("upload\\image\\"+u_name);
session.setAttribute("photofile",photofile);
session.setAttribute("upload_success","上传成功");
response.sendRedirect("index.jsp");
// out.print("上传前路径:"+name+ "<br>文件大小:" + size/1024 + "<a color=red>KB</a><br>");
// out.print("上传后文件路径:"+uploadPath+"\\");
// out.print(u_name);
}
} catch (Exception e) {
out.println(e);
}
} else {
throw new IOException("fail to upload");
}
}
}
} catch (IOException e) {
out.println(e); }
catch (FileUploadException e) {
out.println(e);
}
}
}
public void init() throws ServletException {
}}
是这三种格式相互之间转还是其它的格式转成这三种格式?
如果是前者:6楼的基本上说得比较完全了,先取得文件的文件名后,上传时再加上你想保存的后缀名就可(一般这三种格式的图片可以直接相互改成其它的后缀名,而图片照样正常显示,不信你试试,这三种格式我试了几次都行),再说了,浏览器对这三种格式都支持,为什么要改呢?
如果是后者:那么是什么其它的格式(PSD、CDR、TIFF、PNG等)的图片要转换成这三种格式,其它的都比较少见吧,这想这种情况应该很少见,我们可以在对用户上传文件时过滤其后缀名,如果不是这三种格式的,就不提交。