我的方法如下,
public byte[] readFlie(String filepath) {
InputStream is = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
is = new FileInputStream(filepath);// pathStr 文件路径
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) { out.write(b, 0, n); }// end while } catch (Exception e) {
System.out.println(e); } finally { if (is != null) { try {
out.close();
is.close(); } catch (Exception e) { }// end try }// end if }// end try
return out.toByteArray(); }
我需要通过httppost.webservice上传文件,
需要将文件转换为byte数组,但是文件十兆以内不会有问题,超过了,基本上会内存溢出,请问有什么办法

解决方案 »

  1.   

    和你放去这个项目的服务器性能有关,这样子看你的服务器就只支持那么10MB左右传大文件的时候判断size,超过就不给传,超过就要上传的人分开传(不建议设定为10MB为限制,如果你的这个服务器上还有其他进程占了内存,)
      

  2.   

    你可以读取一定量做一个标记,下次从标记处开始读取。
    类似于:
     
    public byte[] readFlie(String filepath) {
    InputStream is = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
    is = new FileInputStream(filepath);// pathStr 文件路径
    byte[] b = new byte[1024];
    int n;
    int temp = 0;
    while ((n = is.read(b)) != -1) {
    temp ++;
    if(temp >= 100){
      out.write(b, 0, n);
      temp = 0;
    }
    }// end while} catch (Exception e) {
    System.out.println(e);} finally {if (is != null) {try {
    out.close();
    is.close();} catch (Exception e) {}// end try}// end if}// end try
    return out.toByteArray();}
      

  3.   

    JVM大小设置有关吧,看看你的jvm的内存是否设置的太小了
      

  4.   

    在你的webservice 端, 你可以试着用一下 MTOM (Message Transmission Optimization Mechanism),
    这里是个小例子,告诉你怎么用MTOM,
    http://www.mkyong.com/webservices/jax-ws/jax-ws-attachment-with-mtom/
      

  5.   

    再补充一点,建议你也读一读这个
    http://weblogs.java.net/blog/adhirmehta/archive/2010/06/11/transferring-large-binary-data-web-services
      

  6.   

    就是webservice传文件,就是这样一个功能,传递byte[],但是在生成byte数组的时候就会溢出,这个问题该怎么解决,能解决的贴上代码,必须要全部内容传递过去
      

  7.   

    跟这个问题一样的
    http://topic.csdn.net/u/20110612/14/9c5eec66-5054-4309-9525-1281b7f4c98f.html