使用JSP上传的图像被破坏

时间:2017-05-24 10:36:26

标签: java jsp

我有一个JSP页面,用于上传图像。图像上传到服务器但大小减小,文件损坏。我使用下面的代码上传图像文件。

有谁能让我知道我的错误是什么?

String contentType=request.getContentType();

if ((contentType!=null) && contentType.indexOf("multipart/form-data")>=0))
{
    DataInputStream in=new DataInputStream(request.getInputStream());

    int formDataLength=request.getContentLength();

    byte dataBytes[]=new byte[formDataLength];

    int totalBytesRead;

    while(totalBytesRead<formDataLength)
    {
        byteRead=in.read(dataBytes,totalBytesRead,formDataLength);

        totalBytesRead+=byteRead;
    }

    String file=new String(dataBytes);
    String saveFile=file.substring(file.indexOf("filename=\"")+10);

    saveFile=saveFile.substring(0,saveFile.indexOf("\n"));
    saveFile=saveFile.substring(saveFile.lastIndexOf("\\")+1, saveFile.indexOf("\""));

    int lastIndex=contentType.lastIndexOf("=");

    String boundary=contentType.substring(lastIndex+1,contentType.length());

    int pos;

    pos=file.indexOf("filename=\"");
    pos=file.indexOf("\n",pos)+1;
    pos=file.indexOf("\n",pos)+1;
    pos=file.indexOf("\n",pos)+1;

    int boundaryLocation=file.indexOf(boundary,pos);

    int startPos=((file.substring(0,pos)).getBytes()).length;

    int endPos=((file.substring(0,boundaryLocation)).getBytes()).length;

    FileOutputStream fileOut=new FileOutputStream(sDir+"/"+lsNewName);

    fileOut.write(dataBytes,startPos,(endPOs-startPos));

    fileOut.flush();
    fileOut.close();
}

我有两个实例11i和R12,我发现奇怪的是{RID(新系统)中的endPosboundarylocation值不同,但旧系统中的值相同。

我还尝试将Shift-JIS编码应用于endPos,如下所示,它仍然无法正常工作

int endPos=((file.substring(0,boundaryLocation)).getBytes("Shift-JIS")).length;

由于

0 个答案:

没有答案