怎么用org.apache.commons.fileupload上传文件呢
发布时间:2010-05-31 04:14:13 文章来源:www.iduyao.cn 采编人员:星星草
如何用org.apache.commons.fileupload上传文件呢?
给个例子吧
------解决方案--------------------
public class Upload {
private static long lastTime;
private static int n;
public static synchronized String getNo(){
long time=System.currentTimeMillis();
if(time==lastTime){
return time+ "_ "+n++;
}else{
n=0;
lastTime=time;
return time+ "_ "+n;
}
}
private List <FileItem> files=new ArrayList <FileItem> ();
private HashMap <String,String> parameters=new HashMap <String,String> ();
public Upload(HttpServletRequest request) throws FileUploadException{
DiskFileUpload fu = new DiskFileUpload();
fu.setHeaderEncoding( "gb2312 ");
fu.setSizeMax(200000000);
fu.setSizeThreshold(4096);
List fileItems = fu.parseRequest(request);
int length=fileItems.size();
for(int i=0;i <length;i++){
FileItem fi=(FileItem)fileItems.get(i);
if (fi.isFormField()) {
String name = fi.getFieldName();
String value = fi.getString();
parameters.put(name,value);
}else{
files.add(fi);
}
}
}
public Accessory saveFile(FileItem fi) throws Exception{
if(fi.getName()==null||fi.getName().equals( " ")) return null;
String name=null;
String fileName = fi.getName();
name = fileName.substring(fileName.lastIndexOf( "\\ ") + 1 , fileName.length());
String temp=Upload.getNo();
String type=fileName.substring(fileName.lastIndexOf( ". "));
String tempName=temp+type;
File file=new File( "D:/temp/ "+tempName);
// File parent=file.getParentFile();
// if(!parent.exists())parent.mkdirs();
fi.write(file);
Accessory accessory=new Accessory();
accessory.setName(name);
accessory.setTempName(tempName);
accessory.setPath( "D:/temp/ ");
return accessory;
}
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i=0;i <s.length();i++) {
char c = s.charAt(i);
if (c > = 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes( "utf-8 ");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append( "% " + Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}
public Accessory saveFile(FileItem fi,String filePath) throws Exception{
File rootPathFileDir = new File(filePath);
if (rootPathFileDir.length() == 0 && !rootPathFileDir.isDirectory()){
rootPathFileDir.mkdirs();
} else if (!rootPathFileDir.isDirectory()){
rootPathFileDir.mkdirs();
}
filePath = rootPathFileDir.getPath();
filePath = filePath.replaceAll( "\\\\ ", "/ ");
System.out.println(rootPathFileDir.getPath());
String name=null;
String fileName = fi.getName();
name = fileName.substring(fileName.lastIndexOf( "\\ ") + 1 , fileName.length());
String temp=Upload.getNo();
String type=fileName.substring(fileName.lastIndexOf( ". "));
String tempName=temp+type;
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
一段JS代码看不懂
JScript code
function Designer_Builder(designer) {
//属性
this.owner = designer || null;
。。。...
-
共享struts2.1.8+Hibernate3.3+spring2.5.6+extjs3.2的最新版图书管理系统,奋战两周。
写这个系统,是学习的目的,不是真正当项目写...
-
postgreSQL有没有contains函数?
update base_admin set allchildid=allchildid||'00' where CONTAINS(allchildid,'test')
报此错...
-
ssh整合问题,老是提示连接已关闭!
在网上下的一个ssh整合例子,例子是使用的mysql数据库 ,我改成了sqlserver,老是报连接已关闭的错误!高...
-
jsp新手求助,那位大虾进来看看
1楼
我用的是EditPlus来编写jsp页面,我也安装了Tomcat 5.0的jsp引擎,但是我在Editplus编写好了jsp代...
-
apache的HttpClient4.x如何实现跨域访问
请问httpclient如何实现跨域的登录及登录后访问。比如登录为login.xxx.net/login.do,查询...
-
请教一个gson转换的问题
String hql="from VdDicMedicine where c17='1'";
List<VdDicMedicine> list=dao.getList(hql);
...
-
jsp跳转到servlet
[b][/b]晕死啊,我在jsp跳转到servlet这个页面老是出现404错误,我的一些歌环境配置我可以检查了好几遍没发现什么...
-
小弟问个java流读取的问题
image_location是形如/xxxx/image_tmp/2007/6/1/14/的 路径字符串
目的是想 从 /xxxx/image_...
-
struts上传图片
请求帮助 我把图片上传到服务器端,怎么在获得这个图片啊
我获得图片在tomcat下的路径 为什么获得不到图片??
----...