[JSP] File upload

Pagina: 1
Acties:

  • -FoX-
  • Registratie: Januari 2002
  • Niet online

-FoX-

Carpe Diem!

Topicstarter
Ik heb een jsp-formulier (ism struts) die ervoor zorgt dat ik een file kan selecteren en deze kan uploaden.
Aan de server-side vang ik de request op, en schrijf ik de file onder een unieke key weg op de fileserver.

Ik zit alleen met het probleem dat er blijkbaar nooit een file in de request blijkt te zitten en ik zit nu al een dag achter dit probleem te zoeken!

Iemand die soortgelijk probleem misschien herkent?

Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<html:form action="/uploadFile.do" method="POST" enctype="multipart/form-data">
    <table width="100%" border="0" align="center">
        <tr>
            <th align="right"><bean:message key="upload.form.title" />:</th>
            <td><html:text property="title" /></td>
        </tr>
        <tr>
            <th align="right"><bean:message key="upload.form.file" />:</th>
            <td><html:file property="uploadfile" size="40" /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
                <html:submit>
                    <bean:message key="upload.form.submit" />
                </html:submit>
            </td>
        </tr>
    </table>
</html:form>


En in deze actie vang ik deze dan op:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class UploadFileAction extends Action
{
    private static Logger log = Logger.getLogger(UploadFileAction.class);
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, 
HttpServletRequest req, HttpServletResponse res)
    {
        DynaActionForm f = (DynaActionForm) form;
        
        File file = null;       
        log.info("Upload File - init form");
        
        UploadFile uploadfile = new UploadFile();
        file = uploadfile.upload(req);
        
        FileManager manager = new FileManager();
        manager.storeFile(file, manager.generateKey());
        
        return mapping.findForward(ActionConstants.FWD_SUCCESS);
    }


als ik in de action de file uit de request probeer op te halen, krijg ik een mooie null terug... :? :?

[ Voor 30% gewijzigd door -FoX- op 23-05-2004 18:10 ]


  • -FoX-
  • Registratie: Januari 2002
  • Niet online

-FoX-

Carpe Diem!

Topicstarter
Kan toch niet dat niemand een idee heeft?

  • misfire
  • Registratie: Maart 2001
  • Laatst online: 25-05 12:00
Als er nog andere mensen een hint willen hebben wat de oplossing is, op Javahova is er wel een reactie op dit topic.

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 00:01

Janoz

Moderator Devschuur®

!litemod

Ik vind de f.reset op regel 9 erg vreemd. File uploaden met struts doe ik net ietsje anders. In je dynaactionform kun je als het goed is een file type aangeven. Je kunt het bestand vervolgens gewoon uit je form halen ipv dat je met je request aan de slag moet. Ik zit nu helaas niet op mijn werk en kan nu zo snel even niet bij mijn code. Als ik er morgen om denkm zal ik wel ff wat posten.

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


  • -FoX-
  • Registratie: Januari 2002
  • Niet online

-FoX-

Carpe Diem!

Topicstarter
Welke f.reset ? >:) die hoorde daar idd niet.. maar dat zou er niets aan veranderen hoor..

Ik heb het probleem kunnen oplossen dmv gebruik te maken van FormFile. Het probleem was dat ik gelijkertijd gebruik wou maken van het Jakarta commons: fileupload component. En daar wringde het schoentje een beetje.

Nu haal ik de file dus idd rechstreeks uit de form, waarna ik hem verder kan bewerken en naar de juiste plaats op de fileserver loaden.

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 00:01

Janoz

Moderator Devschuur®

!litemod

Ik zal iig de beloofde code nog even posten:
strutsconfig:
XML:
1
2
3
4
        <form-bean name="imageUploadForm" type="org.apache.struts.action.DynaActionForm">
            <form-property name="description" type="java.lang.String"/>
            <form-property name="imagefile" type="org.apache.struts.upload.FormFile"/>
        </form-bean>

action class:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    public ActionForward execute(ActionMapping mapping,
                                       ActionForm form,
                                       HttpServletRequest request,
                                       HttpServletResponse response)
            throws Exception {


        WebAppConfigurator wac = (WebAppConfigurator) ConfigServlet.getConfigurator(ConfigService.WEB_APP_CONFIGURATOR);
        int mw = StringUtils.getIntFromString(wac.getProperty("uploadImage.max_width"), MAX_WIDTH);
        int mh = StringUtils.getIntFromString(wac.getProperty("uploadImage.max_height"), MAX_HEIGHT);
        int mtw = StringUtils.getIntFromString(wac.getProperty("uploadImage.thumb_max_width"), THUMB_WIDTH);
        int mth = StringUtils.getIntFromString(wac.getProperty("uploadImage.thumb_max_height"), THUMB_HEIGHT);


        byte[] thumb;
        byte[] image;
        DynaActionForm daf = (DynaActionForm) form;
        FormFile imageSource = (FormFile) daf.get("imagefile");
        String description = (String) daf.get("description");

        User user = getUser(request);
        int uid = -1;
        if (user != null) uid = user.getId();

        String imageFilename;
        String imageExtention;

        if ((imageSource != null) && (imageSource.getFileSize() > 0)) {
            imageFilename = imageSource.getFileName();
            int index = imageFilename.lastIndexOf(".");
            if (index > -1) {
                imageExtention = imageFilename.substring(index + 1);
                ImageHandler ih = new ImageHandler(imageSource.getFileData(), imageExtention);
                image = ih.getBoundedImage(mw, mh, ih.getImageType());
                thumb = ih.getBoundedImage(mtw, mth, ih.getImageType());
                ImageTO imageTO = new ImageTO(0, description, uid, null, image, thumb);
                imageTO.setExtension(ih.getImageType());
                ImageDAO idao = ImageDAO.createImageDAO();
                idao.insert(imageTO);
            } else {
                log.warn("Wrong filename");
                mapping.findForward("Error");
            }
        } else {
            log.warn("No image found in upload");
            mapping.findForward("Error");
        }
        return mapping.findForward("Success");
    }


Volgens mij maakt Struts zelf intern ook gebruik van jakarta's file upload, misschien ging het daarom niet goed.

[ Voor 5% gewijzigd door Janoz op 24-05-2004 08:36 ]

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'

Pagina: 1