users@jersey.java.net

[Jersey] Upload a file from form input,But hava got file is bad.

From: beanor <beanor_at_gmail.com>
Date: Thu, 26 Feb 2009 23:29:41 -0800 (PST)

Follow is my code:

Class file:

@Path("/upload")
public class UploadResource {
    @Path("/file1")
    @POST
    public void upload(@FormParam("file")
    InputStream file) {
        File f = new File("c:/test.txt");
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos);
            bis = new BufferedInputStream(file);
            byte[] b = new byte[1024];
            int byteread = 0;
            while ((byteread = bis.read(b)) != -1) {
                bos.write(b, 0, byteread);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                fos.close();
                bis.close();
                file.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Jsp file:

<%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="/jerseydemo/resources/upload/file1"
enctype="application/x-www-form-urlencoded" method="post">
<input type="file" name="file" />
<input type="submit" name="submit" value="upload" />
</form>
</body>
</html>

I had uploaded a file cross the jsp page, But I had got the uploaded file
from c:/test.txt,But tthe file is bad(file content is not right),And *.rar
file is not open.

why?
-- 
View this message in context: http://n2.nabble.com/Upload-a-file-from-form-input%2CBut-hava-got-file-is-bad.-tp2394319p2394319.html
Sent from the Jersey mailing list archive at Nabble.com.