不通过网络,就是本地硬盘的文件,应该很简单吧?
解决方案 »
- 大神请进,头大了,我区分不了。。。
- substring的问题
- 请大家帮忙看看为什么这句出错说需要标示符
- 聊天室输出怎么汉字是乱码?如何解决 急
- java中有没有关于Queue的方法或是。。。。。。。。?
- 国内某一著名IT公司面试题,集思广益!!!
- 有关rsa的privateKey问题,已经调了2天了
- 求教关于网络爬虫问题
- 建议:能否把一些经常会有人问的问题,放到一起,比如叫做:java入门之类的
- 为什么我每次启动jbuilder5都要注册,我已经输入了licence和key了?
- 还要请大家帮忙看一下,为什么我的代码在JDK1.408上可以通过并运行,但在JDK1.5中可以编译,但运行就出错呢?
- outOfMemoryError一般怎样改正?
* 创建日期 2005-6-13
*/
package cn.ehoo.wap.util;import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;/**
* @author Dirac
*/
public class UploadFile {
/**
* Uploads images from HttpServletRequest
*
* @param request
* @param basePath
* The path of the web context.
* @return the uploaded image path which is relative to the web context
*/
public static FileModel uploadFile(HttpServletRequest request, String basePath) {
long maxSize = 1048576;
FileModel model = new FileModel();
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(maxSize);
fu.setSizeThreshold(4096);
fu.setRepositoryPath("/temp");
Calendar cal = Calendar.getInstance();
String imagePath = "/upload/" + cal.get(Calendar.YEAR) + "/"
+ (cal.get(Calendar.MONTH) + 1) + "/"
+ cal.get(Calendar.DAY_OF_MONTH) + "/";
String image = "";
try {
List list = fu.parseRequest(request);
Iterator iterator = list.iterator();
String dir = basePath + imagePath;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhMMss");
String path = imagePath + format.format(new Date());
if (!new File(dir).exists()) {
new File(dir).mkdirs();
}
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
String name = item.getName();
long size = item.getSize();
model.setSize(size);
if (size == 0) {
continue;
}
image = path + name.substring(name.lastIndexOf("."));
model.setPath(image);
item.write(new File(basePath + image));
}
} catch (Exception e) {
e.printStackTrace();
}
model.setExt(image.substring(image.indexOf('.')));
return model;
}
}
/*
* 创建日期 2005-7-12
*/
package cn.ehoo.wap.util;/**
* @author Dirac
*/
public class FileModel {
private long size; private int width; private int height; private String path; private String ext; /**
* @return 返回 path。
*/
public String getPath() {
return path;
} /**
* @param path
* 要设置的 path。
*/
public void setPath(String path) {
this.path = path;
} /**
* @return 返回 height。
*/
public int getHeight() {
return height;
} /**
* @param height
* 要设置的 height。
*/
public void setHeight(int height) {
this.height = height;
} /**
* @return 返回 size。
*/
public long getSize() {
return size;
} /**
* @param size
* 要设置的 size。
*/
public void setSize(long size) {
this.size = size;
} /**
* @return 返回 width。
*/
public int getWidth() {
return width;
} /**
* @param width
* 要设置的 width。
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return 返回 ext。
*/
public String getExt() {
return ext;
}
/**
* @param ext 要设置的 ext。
*/
public void setExt(String ext) {
this.ext = ext;
}
}用的是apache的file-upload.jar
Connection conn=java.sql.DriverManager.getConnection(uri,"sa","sa");
File file=new File("C:\\a.txt");
FileInputStream fin=new FileInputStream(file);
PreparedStatement ps;
ps=conn.prepareStatement("insert into test1 values(?,?)");
ps.setInt(1,1);
ps.setBinaryStream(2,fin,(int)file.length());
ps.executeUpdate();
ps.close();
conn.close();