我目前正在尝试Google Cloud提供的一些示例代码,但在编译时遇到了麻烦。以下是我目前正在尝试的示例代码
package com.example;
import com.google.api.client.http.InputStreamContent;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.ObjectAccessControl;
import com.google.api.services.storage.model.Objects;
import com.google.api.services.storage.model.StorageObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.FileOutputStream;
import javax.servlet.*;
import javax.servlet.http.Part;
import javax.servlet.http.*;
public class upload extends HttpServlet {
/*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String Bucket = request.getParameter("Bucket"); // Retrieves <input type="text" name="description">
Part filePart = request.getPart("File"); // Retrieves <input type="file" name="file">
String fileName = filePart.getSubmittedFileName();
InputStream fileContent = filePart.getInputStream();
// ... (do your job here)
}*/
public static void uploadFile(String name, String contentType, File file, String bucketName)
throws IOException, GeneralSecurityException {
InputStreamContent contentStream = new InputStreamContent(
contentType, new FileInputStream(file));
// Setting the length improves upload performance
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject()
// Set the destination object name
.setName(name)
// Set the access control list to publicly read-only
.setAcl(Arrays.asList(
new ObjectAccessControl().setEntity("allUsers").setRole("READER")));
// Do the insert
Storage client = StorageFactory.getService();
Storage.Objects.Insert insertRequest = client.objects().insert(
bucketName, objectMetadata, contentStream);
insertRequest.execute();
}
}
而给我错误的代码是这样的
Storage client = StorageFactory.getService();
我收到的错误消息是
[ERROR] /home/jy/Desktop/cloud2/example/src/main/java/com/example/upload.java:[50,21]
错误:找不到符号[错误]-> [帮助1] org.apache.maven.lifecycle.LifecycleExecutionException:无法执行目标org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile(默认为-在项目示例上编译):编译失败/home/jy/Desktop/cloud2/example/src/main/java/com/example/upload.java:[50,21]错误:找不到符号
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)在org.apache.maven.lifecycle org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)在org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)的org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)的.build(SingleThreadedBuilder.java:51)在org.org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)在org。 org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)处的apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)处的apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)在组织。apache.maven.cli.MavenCli.doMain(MavenCli.java:288)(位于org.apache.maven.cli.MavenCli.main(MavenCli.java:199))位于sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于sun。 org.codehaus.plexus处java.lang.reflect.Method.invoke(Method.java:606)处的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处的reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)上的.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)在org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)在org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)上由:org.apache.maven.plugin.CompilationFailureException:编译失败/home/jy/Desktop/cloud2/example/src/main/java/com/example/upload.java:[66,21]错误:找不到符号
at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:729)
在org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)在org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)在org.apache.maven.lifecycle.internal.MojoExecutor .execute(MojoExecutor.java:207)...另外20个[错误] [错误] [错误]有关错误和可能的解决方案的更多信息,请阅读以下文章:[错误] [帮助1] http:// cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
这是我的pom文件的样子(非常混乱,因为我只是在尝试代码):
<properties>
<appengine.version></appengine.version>
</properties>
<dependencies>
<!-- Compile/runtime dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-1.0-sdk</artifactId>
<version>${appengine.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.21.0</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev65-1.21.0</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.1.7</version>
</dependency>
<dependency>
<groupId>com.google.appengine.tools</groupId>
<artifactId>appengine-gcs-client</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.28</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
<goal>display-plugin-updates</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.5.1</version>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archiveClasses>true</archiveClasses>
<webResources>
<!-- in order to interpolate version from pom into appengine-web.xml -->
<resource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<filtering>true</filtering>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.version}</version>
<configuration>
<enableJarClasses>false</enableJarClasses>
<version>${app.version}</version>
<!-- Comment in the below snippet to bind to all IPs instead of just
localhost -->
<!-- address>0.0.0.0</address> <port>8080</port -->
<!-- Comment in the below snippet to enable local debugging with a remote
debugger like those included with Eclipse or IntelliJ -->
<!-- jvmFlags> <jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
</jvmFlags -->
</configuration>
</plugin>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>gcloud-maven-plugin</artifactId>
<version>${gcloud.plugin.version}</version>
<configuration>
<set_default>true</set_default>
</configuration>
</plugin>
</plugins>
</build>
这是我的Maven版本
Apache Maven 3.3.9(bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)Maven主页:/home/jy/Desktop/apache-maven-3.3.9 Java版本:1.7.0_80,供应商:Oracle Corporation Java主页:/ usr / lib / jvm / java-7-oracle / jre默认语言环境:en_US,平台编码:UTF-8操作系统名称:“ linux”,版本:“ 4.2.0-27-generic”,arch: amd64”,家庭:“ unix”
您可能从GAE官方示例中复制并粘贴了代码。
您应该在Github上检查有关的完整项目代码。的StorageSample
及其uploadFile
方法提供类似的代码。
有关StorageFactory
类不被Maven的编译器(以及任何其他的编译器/ IDE),因为它不依赖GAE的一部分发现的,它是作为样本项目的一部分提供的自定义类:StorageFactory
。
因此,要编译代码,应将其复制到示例项目中。
注意:您可能会遇到进一步的编译错误,最好是检出(或克隆)完整的示例项目并开始使用本地工作副本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句