本站添加帖子中上传文件及图片功能

如:

上传文件:

附件:decomp.zip,575 bytes

上传图片:

普通用户上传限制:

上传文件不大于200K,文件扩展名限于:"zip", "rar", "gif", "jpg", "jpeg", "png";除图片文件,请把非"zip"、"rar"的文件(含压缩文件)用"zip"、"rar"重新压缩一遍上传;本站不对上传文件查毒,请自行负责文件的安全性。

本上传程序是在Struts上传文件例程的基础上并借鉴了http://www.matrix.org.cn的上传页面源文件改写而成。

JSP和JavaScirpt数据交互(转)

(转自:http://www.java3z.com/cwbwebhome/article/article5/5585.jsp?id=912

JSP和JavaScirpt数据交互 点击观看演示

   下载源代码

 

〖 作者:不详 〗〖 大小:2k 〗〖 发布日期:2006-05-31 〗〖 浏览:118 〗

 

 

       对于WEB程序来说,前端(JavaScript)和后端(JSP/Servlet)是没法共用数据的,只能是后端程序(JSP)把数据输出,生成页面到前端,这时候生成的页面中的JavaScript代码才有可能得到所谓jsp的数据。同样的,只有把JavaScript里的数据提交给后端JSP代码,JSP程序中才能得到JavaScript的数据。

    那如何实现在页面的JavaScript中使用jsp中的数据或是在jsp中使用页面的JavaScript数据呢?

一、页面中的JavaScript的数据如何提交给后台的jsp程序

    ① 可以将JavaScript的数据以xxx.JSP?var1=aaa&var2=bbb的形式作为URL的参数传给JSP程序,此时在jsp中用

<%String strVar1=request.getParameter("var1");%>就可以获取到JavaScript脚本传递过来的数据;

    ② 使用JavaScript通过在表单里加入隐藏域信息,然后用表单提交的方式把数据传递给JSP程序。

 

 

二、页面中的JavaScript的数据如何使用后台的JSP程序的数据

    这个比较简单,直接在JavaScript脚本中用<%=strVar1%>就可以把jsp程序中的数据传递给JavaScript脚本使用了。

三、参考下面例子:

1、test.jsp

<%@ page contentType="text/html; charset=gb2312" %>

<%

  String s1="Hello ";

  String s2="World!!";

%>

<script language="JavaScript">

<!--

/***************************************************************

* JavaScript脚本,放置在页面中的任何位置都可以

* insertclick()函数获取JSP传递到页面中的变量s1,

* 然后就可以在JavaScript中修改这个变量的值,再通过

* post的方式提交给JSP程序来使用。

***************************************************************/

function insertclick(){

  var1 ="<%=s1 %>";

  document.forms["insertForm"].mc.value = var1 + document.forms["insertForm"].mc.value ;

  document.insertForm.submit();

}

//-->

</script>

<!-- html页面form表单,放置在html页面中的位置不限 -->

<form name="insertForm" method="post" action="get.jsp">

<!-- 下面这一句是获取JSP程序中传递过来的变量值 -->

<input type="hidden" name="mc" value="<%=s2 %>">

<input type="button" value="提交" onclick="insertclick()">

</form>

2、get.jsp

<%

  String strVar1=request.getParameter("mc");

  out.print(strVar1);

%>

 

iframe的用法(转帖2篇)

(转自:http://www.webshu.com/tutorial/tor/7,id=0408.htm

iframe嵌入网页的用法作者:自由勇点击:MT-8000最后更新 2003-10-12

iframe并不是很常用的,在标准的网页中非常少用。但是有朋友经常问到,下面我简单地介绍一下它的用法,你只要熟练掌握这些参数足矣。

<iframe>也应该是框架的一种形式,它与<frame>不同的是,iframe可以嵌在网页中的任意部分。我们举第一个例子,具体代码如:

<iframe width=420 height=330 frameborder=0 scrolling=auto src=URL></iframe>,这里的URL可以是相对路径,也可以是绝对路径,效果如:

width表示宽度,height表示宽度,可根据实际情况调整。

scrolling表示是否显示页面滚动条,可选的参数为auto、yes、no,如果省略这个参数,则默认为auto。

二、如何实现页面上的超链接指向这个嵌入的网页?

只要给这个iframe命名就可以了。方法是<iframe name=**>,例如我命名为aa,写入这句HTML语言<iframe width=420 height=330 name=aa frameborder=0 src=http://www.cctv.com></iframe>,然后,网页上的超链接语句应该写为:<a href=URL target=aa>

效果如下,请点击这里:中央电视台

三、如果把frameborder设为1,效果就像文本框一样,如下:

网页树树版权声明:

此文为Webshu.com独家撰稿,著作权属作者所有。

传统媒体、公众站点转载请事先联系Webshu编辑。

此文网址 http://www.webshu.com/tutorial/tor/7,id=0408.htm

(转自:http://www.htmlhelp.com/reference/html40/special/iframe.html

The Web Design Group

IFRAME - Inline Frame

Syntax <IFRAME>...</IFRAME>

Attribute Specifications

    * SRC=URI (URI of frame content)

    * NAME=CDATA (name of frame)

    * LONGDESC=URI (link to long description)

    * WIDTH=Length (frame width)

    * HEIGHT=Length (frame height)

    * ALIGN=[ top | middle | bottom | left | right ] (frame alignment)

    * FRAMEBORDER=[ 1 | 0 ] (frame border)

    * MARGINWIDTH=Pixels (margin width)

    * MARGINHEIGHT=Pixels (margin height)

    * SCROLLING=[ yes | no | auto ] (ability to scroll)

    * core attributes

Contents Inline elements, block-level elements

Contained in Block-level elements, inline elements except BUTTON

The IFRAME element defines an inline frame for the inclusion of external objects including other HTML documents. IFRAME provides a subset of the functionality of OBJECT; the only advantage to IFRAME is that an inline frame can act as a target for other links. OBJECT is more widely supported than IFRAME, and, unlike IFRAME, OBJECT is included in HTML 4.0 Strict.

IFRAME's SRC attribute provides the location of the frame content--typically an HTML document. The optional NAME attribute specifies the name of the inline frame, allowing links to target the frame.

The content of the IFRAME element is used as an alternative for browsers that are not configured to show or do not support inline frames. The content may consist of inline or block-level elements, though any block-level elements must be allowed inside the containing element of IFRAME. For example, an IFRAME within an H1 cannot contain an H2, but an IFRAME within a DIV can contain any block-level elements.

The LONGDESC attribute gives the URI of a long description of the frame's contents. This is particularly useful for full descriptions of embedded objects. Note that LONGDESC describes the frame content while the content of the IFRAME element acts as a replacement when the external resource cannot be inlined.

An example follows:

<IFRAME SRC="recipe.html" TITLE="The Famous Recipe">

<!-- Alternate content for non-supporting browsers -->

<H2>The Famous Recipe</H2>

<H3>Ingredients</H3>

...

</IFRAME>

The WIDTH and HEIGHT attributes specify the dimensions of the inline frame in pixels or as a percentage of the available space. The FRAMEBORDER attribute specifies whether or not a border should be drawn. The default value of 1 results in a border while a value of 0 suppresses the border. Style sheets allow greater flexibility in suggesting the border presentation.

The ALIGN attribute specifies the alignment of the inline frame. The values top, middle, and bottom specify the frame's position with respect to surrounding content on its left and right.

ALIGN=middle aligns the center of the frame with the current baseline. To center the frame horizontally on the page, place the frame in a centered block, e.g.,

<P ALIGN=center><IFRAME SRC="foo.html" WIDTH=300 HEIGHT=100></IFRAME></P>

The other ALIGN values, left and right, specify a floating frame; the frame is placed at the left or right margin and content flows around it. To place content below the frame, use <BR CLEAR=left|right|all> as appropriate.

The vertical-align and float properties of Cascading Style Sheets provide more flexible methods of aligning inline frames.

The MARGINWIDTH and MARGINHEIGHT attributes define the number of pixels to use as the left/right margins and top/bottom margins, respectively, within the inline frame. The value must be greater than one pixel.

The SCROLLING attribute specifies whether scrollbars are provided for the inline frame. The default value, auto, generates scrollbars only when necessary. The value yes gives scrollbars at all times, and the value no suppresses scrollbars--even when they are needed to see all the content. The value no should never be used.

More Information

    * IFRAME in W3C HTML 4.0 Recommendation

    * Using inline frames

Maintained by Liam Quinn <liam@htmlhelp.com>

Web Design Group ~ HTML 4.0 Reference ~ Elements by Function ~ Elements Alphabetically

Copyright © 1998-2000 Liam Quinn. All rights reserved.

检讨一下自己,懒人一个

  这两天,老婆把家里认真做了一个大扫除,我也该检讨一下自己是不是平时有点懒?我自己的书桌都基本上不大收拾,别说平时把家里好好收拾一下。

  当然除了过年前的大扫除-我是包干了的。因为老婆过年要加班。

学习在java中计算基本的时间段(转帖)

在目前添加上传文件的功能中需要用到时间相关的操作,这是一篇较详细的介绍,特转帖于此。转自:http://www.cooltang.com/box/topic/character/program/cn-java/0751.htm

学习在java中计算基本的时间段

概述

如果你知道怎样在java中使用日期,那么使用时间和它才不多一样简单。这篇文章告诉你怎样把他们的差别联系起来。Robert Nielsen还告诉你怎样使用java来计算抵达航班和制造过程的时间。

作者:Robert Nielsen

翻译:Cocia Lin 这篇文章是在我发表过的<计算Java时间>(译者:已经翻译完成)的基础上的。在这里,我列出那篇文章几个你应该熟悉得关键点。如果这几点你不太清楚,我建议你读一下<计算Java时间>,了解一下。

1. Java计算时间依靠1970年1月1日开始的毫秒数.

2. Date类的构造函数Date()返回代表当前创建的时刻的对象。Date的方法getTime()返回一个long值在数值上等于1970年1月1日之前或之后的时刻。

3. DateFormat类用来转换Date到String,反之亦然。静态方法getDateInstance()返回DateFormat的缺省格式;getDateInstance(DateFormat.FIELD)返回指定的DateFormat对象格式。Format(Date d)方法返回String表示日期,例如"January 1,2002."反过来,parse(String s)方法返回以参数字符串表示的Date对象。

4. format()方法返回的字符串格式根据不同地区的时间设置而有所不同。

5. GregorianCalendear类有两个重要的构造函数:GregorianCalerdar(),返回代表当前创建时间的对象;GregorianCalendar(int year,int month,int date)返回代表任意日期的对象。GregorianCalendar类的getTime()方法返回日期对象。Add(int field,int amount)方法通过加或减时间单位,象天数,月数或年数来计算日期。

GregorianCalendar和 时间

两个GregorianCalendar的构造函数可以用来处理时间。前者创建一个表示日期,小时和分钟的对象: GregorianCalendar(int year, int month, int date, int hour, int minute) 第二个创建一个表示一个日期,小时,分钟和秒: GregorianCalendar(int year, int month, int date, int hour, int minute, int second) 首先,我应该提醒一下,每一个构造函数需要时间信息中的日期信息(年,月,日)。如果你想说2:30 p.m.,你必须指出日期。

同样,每一个GregorianCalendar构造函数创建一个在时间上使用毫秒计算的对象。所以,如果你的构造函数只提供年,月,日参数,那小时,分钟,秒和毫秒的值将被置0.

DateFormat和时间

你可以使用静态方法getDateTimeInstance(int dateStyle,int timeStyle)来建立DateFormat对象来显示时间和日期。这个方法表明你想要的日期和时间格式。如果你喜欢使用缺省格式,可以使用getDateTimeInstance()来代替它。

你可以使用静态方法getTimeInstance(int timeStyle)创建DateFormat对象来显示正确的时间。

下面的程序示范了getDateTimeInstance()和getTimeInstance()怎样工作: import java.util.*;

import java.text.*; public class Apollo {

public static void main(String[] args) {

GregorianCalendar liftOffApollo11 = new GregorianCalendar(1969, Calendar.JULY, 16, 9, 32);

Date d = liftOffApollo11.getTime();

DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);

DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT);

String s1 = df1.format(d);

String s2 = df2.format(d);

System.out.println(s1);

System.out.println(s2);

}

} 在我的电脑上,上面的程序显示如下: Jul 16, 1969 9:32:00 AM

9:32 AM

(输出根据你所在得地区有所不同) 计算时间间隔

你可能有时需要计算过去的时间;例如,给你开始和结束时间,你想知道制造流程的持续时间。一个出租公司按小时或天数出租东西,计算时间对他们也很有用。同样的,在金融界,经常需要计算重要的支付时间。

将问题复杂化,人类至少是用两种方法计算时间。你可以说一天已经结束当24小时过去了,或者日历从今天翻到明天。我们将讨论我们想到的这两种情况。

时间段,情况 1:严格时间单位

在这种情况中,只有24小时过去,这天才过去,60分钟过去,这个小时才过去,60秒过去,这个分钟才过去,以此类推。在这个方法中,23小时的时间将被认为是0天。

使用这种方法计算时间段,你从计算过去的毫秒开始。为了做到这一点,首先转换每个日期为从1970年1月1日起得毫秒数。你可以从第二个毫秒值中减去第一个毫秒值。这里有一个简单的计算: import java.util.*; public class ElapsedMillis {

public static void main(String[] args) {

GregorianCalendar gc1 = new GregorianCalendar(1995, 11, 1, 3, 2, 1);

GregorianCalendar gc2 = new GregorianCalendar(1995, 11, 1, 3, 2, 2);

// the above two dates are one second apart

Date d1 = gc1.getTime();

Date d2 = gc2.getTime();

long l1 = d1.getTime();

long l2 = d2.getTime();

long difference = l2 - l1;

System.out.println("Elapsed milliseconds: " + difference);

}

} 上面的程序打印如下: Elapsed milliseconds: 1000 这个程序也带来一点混淆。GregorianCalendar类的getTime()返回一个Date对象,Date类的getTime()方法返回从1970年1月1日到这个时间的long类型的毫秒数值。虽然他们的方法名字相同,返回值却不一样!

下面的程序片断用简单的整数除法转换毫秒到秒: long milliseconds = 1999;

long seconds = 1999 / 1000; 这种方法舍去小数部分转换毫秒到秒,所以1,999毫秒等于1秒,2,000毫秒等于2秒。

计算更大的单位-例如天数,小时和分钟-给定一个时间数值,可以使用下面的过程:

1. 计算最大的单位,减去这个数值的秒数

2. 计算第二大单位,减去这个数值的秒数

3. 重复操作直到只剩下秒

例如,如果你的时间的10,000秒,你想知道这个数值相应的是多少小时,多少分钟,多少秒,你从最大的单位开始:小时。10,000除以3600(一个小时的秒数)得到小时数。使用整数除法,答案是2小时(整数除法中小数舍去)计算剩下的秒数,10,000-(3,600 x 2) = 2,800秒。所以你有2小时和2,800秒。

将2,800秒转换成分钟,2,800除以60。使用整数除法,答案是46。2,800 - (60 x 46) = 40秒。最后答案是2小时,46分,40秒。

下面的Java程序使用上面的计算方法: import java.util.*; public class Elapsed1 {

public void calcHMS(int timeInSeconds) {

int hours, minutes, seconds;

hours = timeInSeconds / 3600;

timeInSeconds = timeInSeconds - (hours * 3600);

minutes = timeInSeconds / 60;

timeInSeconds = timeInSeconds - (minutes * 60);

seconds = timeInSeconds;

System.out.println(hours + " hour(s) " + minutes + " minute(s) " + seconds + " second(s)");

} public static void main(String[] args) {

Elapsed1 elap = new Elapsed1();

elap.calcHMS(10000);

}

} 输出结果如下: 2 hour(s) 46 minute(s) 40 second(s) 上面的程序甚至在时间少于一个小时也可以正确的计算小时数。例如,你用上面的程序计算1,000秒,输出入下:

0 hour(s) 16 minute(s) 40 second(s)

举一个现实世界的例子,下面的程序计算阿波罗11飞到月球使用得时间: import java.util.*; public class LunarLanding { public long getElapsedSeconds(GregorianCalendar gc1, GregorianCalendar gc2) {

Date d1 = gc1.getTime();

Date d2 = gc2.getTime();

long l1 = d1.getTime();

long l2 = d2.getTime();

long difference = Math.abs(l2 - l1);

return difference / 1000;

} public void calcHM(long timeInSeconds) {

long hours, minutes, seconds;

hours = timeInSeconds / 3600;

timeInSeconds = timeInSeconds - (hours * 3600);

minutes = timeInSeconds / 60;

System.out.println(hours + " hour(s) " + minutes + " minute(s)" );

} public static void main(String[] args) {

GregorianCalendar lunarLanding = new GregorianCalendar(1969, Calendar.JULY, 20, 16, 17);

GregorianCalendar lunarDeparture = new GregorianCalendar(1969, Calendar.JULY, 21, 13, 54);

GregorianCalendar startEVA = new GregorianCalendar(1969, Calendar.JULY, 20, 22, 56);

GregorianCalendar endEVA = new GregorianCalendar(1969, Calendar.JULY, 21, 1, 9); LunarLanding apollo = new LunarLanding(); long eva = apollo.getElapsedSeconds(startEVA, endEVA);

System.out.print("EVA duration = ");

apollo.calcHM(eva); long lunarStay = apollo.getElapsedSeconds(lunarLanding, lunarDeparture);

System.out.print("Lunar stay = ");

apollo.calcHM(lunarStay);

}

} 上面程序输出如下: EVA duration = 2 hour(s) 13 minute(s)

Lunar stay = 21 hour(s) 37 minute(s) 目前为止,我们计算的基础公式是这样的:1分钟=60秒,1小时=60分,1天=24小时。

"1个月=?天,1年=?天"怎么办?

月份的天数有28,29,30,31;一年可以是365或366天。因此,当你试图计算严格单位的月份和年时,问题就产生了。例如,如果你使用月份的平均天数(近似30.4375),并且计算下面的时间间隔: * July 1, 2:00 a.m. to July 31, 10:00 p.m.

* February 1, 2:00 a.m. to February 29, 10:00 p.m. 第一个计算结果是1个月;第二个结果是0个月!

所以,在计算严格单位时间的月份和年份是要想好。

时间段,情况 2:时间单位变化

时间单位的变化相当的简单:如果你要统计天数,你可以简单的统计日期变化次数。例如,如果某事15日开始,17日结束,经过2天。(日期先是便到16,再到17)同样的,一个步骤下午3:25开始,4:10 p.m结束,历时1个小时,因为小时数值变了一次(从3到4)。

图书馆经常使用这种习惯计算时间。例如,如果你从图书馆接一本书,我不能占有这本书最少24小时,会认为图书馆这样才给你算一天。而是,我的账号上记录我借书的日期。日期以变成下一天,我就已经结这本书一天了,即使总计不足24小时。

当使用单位的变化来计算时间段,通常感觉计算的时间没有多于一个时间单位。例如,如果9:00 p.m.我借了一本图书馆的书,第二天中午还回去,我能算出我借了这本书一天了。可是,有一种感觉在问:"1天和几个小时呢?"这本说总计借出15个小时,答案是一天还差9个小时呢?因此,这篇文章里,我将以一个时间单位变化计算时间。

单位变化的时间算法

这是你怎样计算两个日期的时间变化:

1. 制作两个日期的拷贝。Close()方法能制作拷贝。

2. 使用日期拷贝,将所有的小于时间单位变化的部分设置成它的最小单位。例如,如果计算天数,那么将小时,分钟,秒和毫秒设置成0。这种情况中,使用clear()方法将时间值设置称他们各自的最小值。

3. 取出较早的日期,将你要计算的单位加1,重复直到两个日期相等。你加1的次数就是答案。可以使用before()和after()方法,他们返回boolean值,来判断是否一个日期在另一个日期之前或之后。

下面的类的方法用来计算天数和月数。 import java.util.*; public class ElapsedTime { public int getDays(GregorianCalendar g1, GregorianCalendar g2) {

int elapsed = 0;

GregorianCalendar gc1, gc2; if (g2.after(g1)) {

gc2 = (GregorianCalendar) g2.clone();

gc1 = (GregorianCalendar) g1.clone();

}

else {

gc2 = (GregorianCalendar) g1.clone();

gc1 = (GregorianCalendar) g2.clone();

} gc1.clear(Calendar.MILLISECOND);

gc1.clear(Calendar.SECOND);

gc1.clear(Calendar.MINUTE);

gc1.clear(Calendar.HOUR_OF_DAY); gc2.clear(Calendar.MILLISECOND);

gc2.clear(Calendar.SECOND);

gc2.clear(Calendar.MINUTE);

gc2.clear(Calendar.HOUR_OF_DAY); while ( gc1.before(gc2) ) {

gc1.add(Calendar.DATE, 1);

elapsed++;

}

return elapsed;

} public int getMonths(GregorianCalendar g1, GregorianCalendar g2) {

int elapsed = 0;

GregorianCalendar gc1, gc2; if (g2.after(g1)) {

gc2 = (GregorianCalendar) g2.clone();

gc1 = (GregorianCalendar) g1.clone();

}

else {

gc2 = (GregorianCalendar) g1.clone();

gc1 = (GregorianCalendar) g2.clone();

} gc1.clear(Calendar.MILLISECOND);

gc1.clear(Calendar.SECOND);

gc1.clear(Calendar.MINUTE);

gc1.clear(Calendar.HOUR_OF_DAY);

gc1.clear(Calendar.DATE); gc2.clear(Calendar.MILLISECOND);

gc2.clear(Calendar.SECOND);

gc2.clear(Calendar.MINUTE);

gc2.clear(Calendar.HOUR_OF_DAY);

gc2.clear(Calendar.DATE); while ( gc1.before(gc2) ) {

gc1.add(Calendar.MONTH, 1);

elapsed++;

}

return elapsed;

}

} 你可以在上面的类中补充另外的方法来处理小时和分钟。同样,计算时间段的算法能更高效一些,尤其是时间相隔很长。可是,作为介绍目的,这个算法有短小和简单的优势。

下面的例子使用ElapsedTime类来计算两个日期之间的天使,而后是月数: import java.util.*; public class Example {

public static void main(String[] args) {

GregorianCalendar gc1 = new GregorianCalendar(2001, Calendar.DECEMBER, 30);

GregorianCalendar gc2 = new GregorianCalendar(2002, Calendar.FEBRUARY, 1); ElapsedTime et = new ElapsedTime();

int days = et.getDays(gc1, gc2);

int months = et.getMonths(gc1, gc2); System.out.println("Days = " + days);

System.out.println("Months = " + months);

}

} 当计算时,上面的程序可能有用,例如,最近的航班。它显示下面的输出: Days = 33

Months = 2 (OK,关于航班的计算有些夸张;这个天数算法很适合像图书馆借书这样的应用,你看到了她怎样工作)

告诫

在进行时间工作时要谨慎:你看到的时间段的例子,你精确仔细的考虑非常重要。本文介绍了两种通常计算时间段的想法,但是人们能想到的时间段的计算方法仅仅受到人类想象力的限制。

所以,当写一个Java程序的时候,确信你的精确度能让使用和以来这些程序的人满意。同样,彻底的测试程序对处理时间的程序非重重要。

总结

本文是在我的前一篇文章 Java时间计算介绍怎样使用GregorianCalendar 和 DateFormat类处理时间问题的基础上的。你已经看到了两种方法来思考时间段问题和两种相应的途径使用Java来处理时间问题。这里提供的信息,很基础,提供给你一个在Java中处理时间问题的有力工具。关于作者

Robert Nielsen是SCJP。他拥有硕士学位,专攻计算机教育,并且在计算机领域执教多年。他也在各样的杂志上发表过很多计算机相关的文章。

关于译者

Cocia Lin(cocia@163.com)是程序员。它拥有学士学位,现在专攻Java相关技术,刚刚开始在计算机领域折腾。

java的时间精度(使用Real-Time Java编写Real-Time程序)(转帖)

节自:http://www.huihoo.com/rt_embed/rt/rtj/rtj_rta.html

这里所说的高精度时间,是相对于普通java而言。普通java的时间精度是毫秒,而rtsj的高精度时间可表示的精度为纳秒。

高精度时间用一个64位表示的毫秒值和一个32位表示的纳秒值组成,时间长度就是:

10e64 (毫秒) + 10e32 * 1000 (纳秒) = 时间总和 (毫秒)

这个时间总和相当于大约2.92亿年。

这样,从时间精度上和时间表示长度上,都对以前的java时间做了很大的提升。

不过,现在的软件系统能够响应1个毫秒级也决非易事,更何况是纳秒了。这可能是rtsj规范的制定者对千年虫问题还是心有余悸的缘故吧。

jsp上传文件;jsp上传含中文名的文件(转帖)

转自:http://www.linuxsir.org/bbs/archive/index.php/t-167492.html

LinuxSir.Org > 编程开发讨论区 —— LinuxSir.Org > Java 程序设计开发讨论 > 我想用jsp上传含中文名的文件,该怎么办??

--------------------------------------------------------------------------------

PDA查看完整版本 : 我想用jsp上传含中文名的文件,该怎么办??

--------------------------------------------------------------------------------

pilchard04-12-30, 11:24

上传到服务器以后中文显示不出来?

--------------------------------------------------------------------------------

eTony04-12-30, 11:42

我用的 o'reilly 的上传组件 没有问题

--------------------------------------------------------------------------------

nscwf04-12-30, 12:06

我有个办法,把文件名用POST记下来,上传之后,再更名回来

--------------------------------------------------------------------------------

hantsy04-12-30, 13:42

我用struts上传没有问题,

--------------------------------------------------------------------------------

fangshun05-01-06, 08:47

上传的字节流转换String时需要转码!!!

--------------------------------------------------------------------------------

fangshun05-01-06, 08:53

/**

* 文件上传进行代理服务,阻止不符合上传规范的文件进入。

* 使用单实例创建,外部调用只有通过工厂方法才可以创建唯一的代理实例。

* @author fangshun

* @see FileUploadFactroy

*/

public class FileUploadProxy implements FileUpload {

/**

* 外部调用只有通过工厂方法才可以创建唯一的代理实例。

*/

protected FileUploadProxy() {}

/**

* 上传方法<tt>upload</tt>的代理验证,阻止不符合规范的上传方式,

* 实现接口的方法。

* @param request 需要得到从上传页面传来的request对象。

*

* @param pathName 服务器存放上传文件的绝对路径,

* 这可能来自你自己的指定,或者来自你要解析的某个已经配置好的配置文件

* 例如WEB-INF下的某个目录的xml配置文件

*

* @param sizeMax 需要上传的文件总体的容量,这里使用long类型,以满足

* 能支持大容量的文件上传,请注意:上传参数sizeMax的最小单位为'K'

*

* @throws Exception

* @see FileUpload

*/

public void upload(HttpServletRequest request, String pathName, long sizeMax)

throws Exception {

// TODO Auto-generated method stub

if ((request == null) || (pathName == null)) {

throw new NullPointerException("parameter");

}

if (sizeMax < 0) {

throw new FileSizeException("File Require Size ");

} else {

//从request中获取上下文内容尺度

int requestSize = request.getContentLength();

//为传来的最大容量乘以1024,获得符合以'K'为单位的尺度

long sizeMax_K = sizeMax << 10;

//String pathNameCN = new String(pathName.getBytes("utf-8"), "gb2312");

//如果传来的尺度大于上传的最大限度

if (requestSize > sizeMax_K) {

throw new SizeLimitExceededException(

"the request was rejected because " +

"it's size exceeds allowed range");

}

String contentType = request.getContentType();

//如果传来的条件符合代理类的要求,执行上传操作!!!

JetStepFileUpload upload = JetStepFileUpload.getInstance();

upload.processUpload(request, pathName, sizeMax_K);

}

}

/**

* 上传实现细节

* 内隐类实现

* @author fangshun

*/

private static class JetStepFileUpload {

private static JetStepFileUpload upload;

/**

* 外界对象不能进行实例化,私有创建对象。

*/

private JetStepFileUpload() { }

private static JetStepFileUpload getInstance() {

if(upload == null) {

upload = new JetStepFileUpload();

}

return upload;

}

/**

* 上传实现

* @param request 需要得到从上传页面传来的request对象。

*

* @param pathName 服务器存放上传文件的绝对路径,

* 这可能来自你自己的指定,或者来自你要解析的某个已经配置好的配置文件

* 例如WEB-INF下的某个目录的xml配置文件

*

* @param sizeMax 需要上传的文件总体的容量,这里使用long类型,以满足

* 能支持大容量的文件上传,参数sizeMax的最小单位为'K'

* @throws Exception

*/

private void processUpload(HttpServletRequest request, String filePath, long sizeMax)

throws Exception {

DiskFileUpload fu = new DiskFileUpload();

String encoding = request.getCharacterEncoding();

fu.setHeaderEncoding(encoding);

// If file size exceeds, a FileUploadException will be thrown

fu.setSizeMax(sizeMax);

List fileItems = fu.parseRequest(request);

Iterator itr = fileItems.iterator();

while (itr.hasNext()) {

FileItem fi = (FileItem) itr.next();

String fileName = fi.getName();

if(fileName == null || fileName.equals("")) {

continue;

}

System.out.println("no convert" + fileName);

//fileName = new String(fileName.getBytes(), "utf-8");

// fileName = new String(fileName.getBytes(), "gb2312");

System.out.println("fileName" + fileName);

//Check if not form field so as to only handle the file inputs

//else condition handles the submit button input

if (!fi.isFormField()) {

File file = new File(fileName);

File fNew = new File(filePath, file.getName());

fi.write(fNew);

} else {

System.out.println("Field =" + fi.getFieldName());

}

}

}

}

}

--------------------------------------------------------------------------------

L0veyou05-01-07, 20:36

这是一个用Bean的文件上传JSP,可以识别各种编码.

uploadfile.jsp:

<%@ page language="java"%>

<%@ page contentType="text/HTML;charset=GB2312"%>

<jsp:useBean id="fub" scope="page" class="mybean.FileUploadBean" />

<%

String Dir = "D:\\WWW\\testing\\test"; //保存文件的路径,请确保目录存在,或改到其他目录

//通过调用JavaBeans的方法完成上传操作

fub.setUploadDirectory(Dir);

fub.uploadFile(request);

out.print("<center><font color=red>文件上载成功!</font></center>");

%>

FileUploadBean.java

package mybean;

import java.io.*;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.ServletInputStream;

import javax.servlet.ServletException;

public class FileUploadBean{

private static String newline = "\r\n"; //设定换行符

private String uploadDirectory = "."; //默认的保存位置

private String ContentType = ""; //文件类型

private String CharacterEncoding = ""; //编码格式

//设定文件要保存在服务器中的位置

public void setUploadDirectory(String s){

uploadDirectory = s;

}

//提取文件名,本方法是把用户上传的文件按照原名保存

private String getFileName(String s){

int i = s.lastIndexOf("\\");

if(i < 0 || i >= s.length() - 1){

i = s.lastIndexOf("/");

if(i < 0 || i >= s.length() - 1)

return s;

}

return s.substring(i + 1);

}

//得到content-type

public void setContentType(String s){

ContentType = s;

int j;

if((j = ContentType.indexOf("boundary=")) != -1){

ContentType = ContentType.substring(j + 9);

ContentType = "--" + ContentType;

}

}

//得到文件编码格式

public void setCharacterEncoding(String s){

CharacterEncoding = s;

}

public void uploadFile( HttpServletRequest req) throws ServletException, IOException{

setCharacterEncoding(req.getCharacterEncoding());

setContentType(req.getContentType());

uploadFile(req.getInputStream());

}

public void uploadFile( ServletInputStream servletinputstream) throws ServletException, IOException{

String s5 = null;

String filename = null;

byte Linebyte[] = new byte[4096];

byte outLinebyte[] = new byte[4096];

int ai[] = new int[1];

int ai1[] = new int[1];

String line;

//得到文件名

while((line = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){

int i = line.indexOf("filename=");

if(i >= 0){

line = line.substring(i + 10);

if((i = line.indexOf("\"")) > 0)

line = line.substring(0, i);

break;

}

}

filename = line;

if(filename != null && !filename.equals("\"")){

filename = getFileName(filename);

String sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding);

if(sContentType.indexOf("Content-Type") >= 0)

readLine(Linebyte, ai, servletinputstream, CharacterEncoding);

//建立新文件的文件句柄

File file = new File(uploadDirectory, filename);

//建立生成新文件的输出流

FileOutputStream fileoutputstream = new FileOutputStream(file);

while((sContentType = readLine(Linebyte, ai, servletinputstream, CharacterEncoding)) != null){

if(sContentType.indexOf(ContentType) == 0 && Linebyte[0] == 45)

break;

if(s5 != null){

//写入新文件

fileoutputstream.write(outLinebyte, 0, ai1[0]);

fileoutputstream.flush();

}

s5 = readLine(outLinebyte, ai1, servletinputstream, CharacterEncoding);

if(s5 == null || s5.indexOf(ContentType) == 0 && outLinebyte[0] == 45)

break;

fileoutputstream.write(Linebyte, 0, ai[0]);

fileoutputstream.flush();

}

byte byte0;

if(newline.length() == 1)

byte0 = 2;

else

byte0 = 1;

if(s5 != null && outLinebyte[0] != 45 && ai1[0] > newline.length() * byte0)

fileoutputstream.write(outLinebyte, 0, ai1[0] - newline.length() * byte0);

if(sContentType != null && Linebyte[0] != 45 && ai[0] > newline.length() * byte0)

fileoutputstream.write(Linebyte, 0, ai[0] - newline.length() * byte0);

fileoutputstream.close();

}

}

//readLine函数把表单提交上来的数据按行读取

private String readLine(byte Linebyte[], int ai[],ServletInputStream servletinputstream,String CharacterEncoding){

try{ //读取一行

ai[0] = servletinputstream.readLine(Linebyte, 0, Linebyte.length);

if(ai[0] == -1)

return null;

}catch(IOException ex){

return null;

}

try{

if(CharacterEncoding == null){

//用默认的编码方式把给定的byte数组转换为字符串

return new String(Linebyte, 0, ai[0]);

}else{

//用给定的编码方式把给定的byte数组转换为字符串

return new String(Linebyte, 0, ai[0], CharacterEncoding);

}

}catch(Exception ex){

return null;

}

}

}

--------------------------------------------------------------------------------

vBulletin v3.5.4,版权所有 ©2000-2006,Jelsoft Enterprises Ltd.