如何使用JSP/Servlet将文件上传到服务器?我试过这个:
<form action="upload" method="post">
<input type="text" name="description" />
<input type="file" name="file" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
但是,我只获取文件名,而不是文件内容.当我添加 enctype="multipart/form-data"到<form>,然后request.getParameter()返回null.
在研究期间,我偶然发现了Apache Common FileUpload.我试过这个:
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request); // This line is where it died.
Run Code Online (Sandbox Code Playgroud)
不幸的是,servlet抛出了一个没有明确消息和原因的异常.这是堆栈跟踪:
SEVERE: Servlet.service() for servlet UploadServlet threw exception
javax.servlet.ServletException: Servlet execution threw an exception
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at …Run Code Online (Sandbox Code Playgroud) 我在使用Ajax提交包含UTF-8字符串的表单时遇到问题.我正在开发一个在Tomcat服务器上运行的Struts Web应用程序.这是我设置为使用UTF-8的环境:
我已经添加的属性URIEncoding="UTF-8" useBodyEncodingForURI="true"到Connector标签到Tomcat的conf/server.xml文件.
我有一个utf-8_general_ci数据库
我正在使用下一个过滤器来确保我的请求和响应以UTF-8编码
package filters;
import java.io.IOException;
import javax.servlet.*;
public class UTF8Filter implements Filter {
public void destroy() {}
public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
Run Code Online (Sandbox Code Playgroud)我在WEB-INF/web.xml中使用此过滤器
我正在使用我的JSON响应的下一个代码:
public static void populateWithJSON(HttpServletResponse response,JSONObject json)
{
String CONTENT_TYPE="text/x-json;charset=UTF-8";
response.setContentType(CONTENT_TYPE);
response.setHeader("Cache-Control", "no-cache");
try {
response.getWriter().write(json.toString());
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud)我有一个非常简单的JSP/Servlet 3.0/Spring MVC 3.1应用程序.
在我的一个页面上,我有多种形式.其中一种形式允许用户上传文件,因此配置文件enctype="multipart/form-data".我在web.xml文件multipart-config中使用自Servlet 3.0以来可用的元素配置了multipart上传,并结合了<bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>我的spring配置.
我也org.springframework.web.filter.CharacterEncodingFilter配置了Spring .
我遇到的问题是我找不到将StandardServletMultipartResolver的默认编码设置为UTF-8的方法,这通常会导致多部分表单中文本字段的内容全部出现乱码.
有没有什么办法解决这一问题?
提前致谢.
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>foo-web</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF\applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>foo</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>0</load-on-startup>
<multipart-config>
<max-file-size>52428800</max-file-size>
<file-size-threshold>5242880</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>foo</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)
我有一个问题,我真的不明白.我试图在asp经典应用程序中上传文件,而不使用外部组件.我还想发布一些将存储在DB中的文本.文件上传完美,我正在使用此代码:Lewis E. Moten III上传没有COM v3的文件
问题是其他形式的输入字段.我使用的是UTF-8,但它们最终并不是UTF-8.即如果我使用Response.Write将它们打印出来,瑞典字符åä和ö将显示为问号.
我已经将文件保存为UTF-8(带有BOM),我已经添加了元标记来告诉页面它是UTF-8.我设置了Response.CharSet ="UTF-8".
从二进制转换为字符串的函数看起来像这样(这是我唯一能想到的可能是错误的地方,因为注释说它会拉出ANSI字符,但我认为它应该拉出Unicode字符):
Private Function CStrU(ByRef pstrANSI)
' Converts an ANSI string to Unicode
' Best used for small strings
Dim llngLength ' Length of ANSI string
Dim llngIndex ' Current position
' determine length
llngLength = LenB(pstrANSI)
' Loop through each character
For llngIndex = 1 To llngLength
' Pull out ANSI character
' Get Ascii value of ANSI character
' Get Unicode Character from Ascii
' Append character to results
CStrU …Run Code Online (Sandbox Code Playgroud) forms iis-7 multipartform-data character-encoding asp-classic
我在我的Web应用程序(JSF 1.2,Spring和Tomcat 7)中遇到charset编码问题,而且我已经用尽了测试内容以查看它出错的地方.
每当我提交类似'çã'的内容时,我会得到'çã':这意味着我在UTF-8上发布的数据在JSF生命周期的某个地方被转换为ISO-8859-1.
我知道错误的转换是UTF-8到ISO-8859-1,因为它的输出相同:
System.out.println(new String("çã".getBytes("UTF-8"), "ISO-8859-1"));
Run Code Online (Sandbox Code Playgroud)
我相信错误的转换是在JSF生命周期中的某个地方(它可以在之前吗?)因为我在我的MB中设置了一个验证器:
public void debugValidator(FacesContext context, UIComponent component,
Object object) throws ValidationException {
System.out.println("debug validator:");
System.out.println(object);
System.out.println("\n");
throw new ValidationException("DEBUG: " + object.toString());
}
Run Code Online (Sandbox Code Playgroud)
并且它的消息返回:"DEBUG:çã"
<?xml version="1.0" encoding="UTF-8"?>.CharacterEncodingFilter在我的web.xml中将请求字符编码设置为UTF-8.URIEncoding="UTF-8"Tomcat的server.xml文件,只是为了保证你知道我还能测试什么吗?可能是我的错误假设?
提前致谢!
我使用dropwizard实现我的服务器,并且为了上传文件,我使用此答案使用多部分formdata 。
但是当我使用带有utf-8字符集的文件时,来自FormDataContentDisposition对象的文件名崩溃了。
码:
@POST
@Path("/")
@Consumes({MediaType.MULTIPART_FORM_DATA + ";charset=utf-8"})
public void fileUploaded(@AuthRequired User admin,
@FormDataParam("file") final InputStream inputStream,
@FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) {
System.out.println(contentDispositionHeader.getFileName());
}
Run Code Online (Sandbox Code Playgroud) 我想知道为什么 spring boot 将 MultiPartFile 文件名特殊字符转换为 ?(例如 \xc3\xa9\xc3\xa9\xc3\xa9.pdf 转换为 ???.pdf)。我需要配置 Spring 来禁用此行为吗?我已经检查了我的 jvm 配置中的 file.encoding,它已经设置为 UTF-8。
\n\n我这样执行文件上传:
\n\n@PostMapping("/upload")\npublic void uploadFile(@RequestParam MultipartFile file){\n// todo : ...\n}\nRun Code Online (Sandbox Code Playgroud)\n java ×3
jsp ×3
file-upload ×2
utf-8 ×2
ajax ×1
asp-classic ×1
dropwizard ×1
encoding ×1
facelets ×1
forms ×1
iis-7 ×1
java-ee ×1
jquery ×1
jsf ×1
servlet-3.0 ×1
servlets ×1
spring ×1
spring-boot ×1
spring-mvc ×1
tomcat7 ×1